Dale Roberts, Lecturer - PowerPoint PPT Presentation

About This Presentation
Title:

Dale Roberts, Lecturer

Description:

School of Science, IUPUI. CSCI 230. Data Structures. Dale ... back to the first node. Doubly linked list. Two 'start pointers' first element and last element ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 41
Provided by: jas5131
Category:
Tags: back | dale | deals | lecturer | roberts | school | to

less

Transcript and Presenter's Notes

Title: Dale Roberts, Lecturer


1
Department of Computer and Information
Science,School of Science, IUPUI
CSCI 230
Data Structures
  • Dale Roberts, Lecturer
  • Computer Science, IUPUI
  • E-mail droberts_at_cs.iupui.edu

2
Data Structure
  • Algorithms deals with the manipulation of data
  • Data structure deals with the organization of
    data
  • Algorithms Data Structures Programs
  • Suppose we have a list of sorted data on which we
    have to perform the following operations
  • Search for an item
  • delete a specified item
  • insert (add) a specified item
  • Example suppose we begin with the following
    list
  • data 345 358 490 501 513 555 561
    701 724 797
  • location 0 1 2 3 4
    5 6 7 8 9
  • What is a list?
  • A list is a data structure where data is
    represented linearly. A list can be implemented
    using arrays on a machine.
  • Finite sequence of items from the same data type
  • Stored contiguously in the memory

3
  • Example suppose we begin with the following
    list
  • data 345 358 490 501 513 555 561
    701 724 797
  • location 0 1 2 3 4
    5 6 7 8 9
  • Now, delete item 358 from the above list
  • Q What is the algorithm to delete an item?
  • Q What is the cost of deleting an item?
  • data 345 358 490 501 513 555 561
    701 724 797
  • location 0 1 2 3 4
    5 6 7 8 9
  • Q When we delete 358, what happens to that
    location?
  • Now, add item 498 onto the above list
  • Q Where would that item go?
  • data 345 358 490 501 513 555 561
    701 724 797
  • location 0 1 2 3 4
    5 6 7 8 9
  • Q What is the cost of inserting an item?
  • Conclusion
  • Using a list representation of data, what is the
    overall efficiency of searching, adding, and
    deleting items?

498
4
Linked Representation of Data
  • In a linked representation, data is not stored in
    a contiguous manner. Instead, data is stored at
    random locations and the current data location
    provides the information regarding the location
    of the next data.
  • Adding item 498 on to the linked list
  • Q What is the cost of adding an item?
  • Q how about adding 300 and 800
  • onto the linked list
  • Deleting item 358 from the linked list
  • Q What is the cost of deleting an item?
  • Q What is the cost of searching for an item?

5
Deletion of an Element from a List
  • Algorithm
  • locate the element in the list (this involves
    searching)
  • delete the element
  • reorganize the list and index
  • Example
  • data 345 358 490 501 513 555
    561 701 724 797
  • location 0 1 2 3 4 5
    6 7 8 9
  • Delete 358 from the above list
  • Locate 358 if we use linear search, well
    compare 358 with each element of the list
    starting from the location 0.
  • Delete 358 remove it from the list
  • data 345 490 501 513 555
    561 701 724 797
  • location 0 1 2 3 4 5
    6 7 8 9
  • Reorganize the list move the remaining
    elements.
  • data 345 490 501 513 555 561
    701 724 797
  • location 0 1 2 3 4 5 6
    7 8

6
Insertion of an Element in List
  • Algorithm
  • locate the position where the element in to be
    inserted (position may be user-specified in case
    of an unsorted list or may be decided by search
    for a sorted list)
  • reorganize the list and create an empty slot
  • insert the element
  • Example (sorted list)
  • data 345 358 490 501 513 555
    561 701 724 797
  • location 0 1 2 3 4 5 6
    7 8 9
  • Insert 505 onto the above list
  • Locate the appropriate position by performing a
    binary search. 505 should be stored in location
    4.
  • Create an empty slot
  • data 345 358 490 501 513 555
    561 701 724 797
  • location 0 1 2 3 4 5
    6 7 8 9 10
  • Insert 505
  • data 345 358 490 501 505 513 555
    561 701 724 797
  • location 0 1 2 3 4 5 6
    7 8 9 10

7
Introduction
  • Dynamic data structures
  • Data structures that grow and shrink during
    execution
  • Linked lists
  • Allow insertions and removals anywhere
  • Stacks
  • Allow insertions and removals only at top of
    stack
  • Queues
  • Allow insertions at the back and removals from
    the front
  • Binary trees
  • High-speed searching and sorting of data and
    efficient elimination of duplicate data items

8
Linked Lists
  • Linked list
  • Linear collection of self-referential class
    objects, called nodes
  • Connected by pointer links
  • Accessed via a pointer to the first node of the
    list
  • Subsequent nodes are accessed via the
    link-pointer member of the current node
  • Link pointer in the last node is set to null to
    mark the lists end
  • Use a linked list instead of an array when
  • You have an unpredictable number of data elements
  • Your list needs to be sorted quickly

9
Linked List
  • How do we represent a linked list in the memory
  • we use pointers to point to the next data item.
    Each location has two fields Data Field and
    Pointer (Link) Field.
  • Linked List Implementation
  • struct node
  • int data
  • struct node link
  • struct node my_node
  • Example

START
Node Element
Pointer (Link) Field
Data Field
Null Pointer
1
2
NULL
3
3
4
5
10
Self-Referential Structures
  • Self-referential structures
  • Structure that contains a pointer to a structure
    of the same type
  • Can be linked together to form useful data
    structures such as lists, queues, stacks and
    trees
  • Terminated with a NULL pointer (0)
  • Diagram of two self-referential structure objects
    linked together
  • struct node int data struct node
    nextPtr
  • nextPtr
  • Points to an object of type node
  • Referred to as a link
  • Ties one node to another node

11
Linked Lists
  • Types of linked lists
  • Singly linked list
  • Begins with a pointer to the first node
  • Terminates with a null pointer
  • Only traversed in one direction
  • Circular, singly linked
  • Pointer in the last node points
  • back to the first node
  • Doubly linked list
  • Two start pointers first element and last
    element
  • Each node has a forward pointer and a backward
    pointer
  • Allows traversals both forwards and backwards
  • Circular, doubly linked list
  • Forward pointer of the last node points to the
    first node and backward pointer of the first node
    points to the last node

12
Linked List Manipulation Algorithms
  • List Traversal
  • Let START be a pointer to a linked list in
    memory. Write an algorithm to print the contents
    of each node of the list
  • Algorithm
  • set PTR START
  • repeat step 3 and 4 while PTR ? NULL
  • print PTR-gtDATA
  • set PTR PTR -gt LINK
  • stop

10
20
30
40
1000
2000
3000
4000
10
20
30
40
50
START
Data
Link
PTR LINKPTR
PTR
13
ptrNULL / List Traversal / printf(sequent
ial print of the list\n) ptrhead while
(ptr-gtnext ! NULL) printf(The value is
d\n, ptr-gtdata) ptrptr-gtnext input
5 different data 5 4 3 2 9 sequential
print of the list The value is 5 The value
is 4 The value is 3 The value is 2 The
value is 9
  • / basic link list application /
  • / forward order /
  • define NULL 0
  • struct list
  • int data
  • struct list next
  • typedef struct list node
  • typedef node link
  • main()
  • link ptr, head
  • int num, i
  • head (link)malloc(sizeof(node))
  • ptrhead
  • printf(input 5 different data\n)

14
ptrptr-gtnext / List Traversal
/ printf(reverse print of the list\n) while
(ptr-gtnext ! NULL) printf(The value is
d\n, ptr-gtdata) ptrptr-gtnext input
5 different data 5 4 3 2 9 sequential
print of the list The value is 9 The value
is 2 The value is 3 The value is 4 The
value is 5
  • / basic link list application /
  • / reverse order /
  • define NULL 0
  • struct list
  • int data
  • struct list next
  • typedef struct list node
  • typedef node link
  • main()
  • link ptr, tail, head
  • int num, i
  • tail (link)malloc(sizeof(node))
  • tail-gtnext NULL
  • ptrtail

15
  • Search for a ITEM
  • Let START be a pointer to a linked list in
    memory. Write an algorithm that finds the
    location LOC of the node where ITEM first appears
    in the list, or sets LOCNULL if search is
    unsuccessful.
  • Algorithm
  • set PTR START
  • repeat step 3 while PTR ? NULL
  • if ITEM PTR -gt DATA, then
  • set LOC PTR, and Exit
  • else
  • set PTR PTR -gt LINK
  • set LOC NULL /search unsuccessful /
  • Stop

16
  • Insertion into a Listed List
  • Let START be a pointer to a linked list in memory
    with successive nodes A and B. Write an
    algorithm to insert node N between nodes A and B.
  • Algorithm
  • Set PTR START
  • Repeat step 3 while PTR ? NULL
  • If PTR A, then
  • Set N-gtLINK PTR -gt LINK (or B)
  • Set PTR-gtLINK N
  • exit
  • else
  • Set PTRPTR-gtLINK
  • If PTR NULL insertion unsuccessful
  • Stop

3 cases first node, last node, in-between node.
(ex if ITEM 500? if ITEM 6000?)
17
  • Deletion from a Linked List
  • Let START be a pointer to a linked list in memory
    that contains integer data. Write an algorithm
    to delete note which contains ITEM.
  • Algorithm
  • Set PTRSTART and TEMP START
  • Repeat step 3 while PTR ? NULL
  • If PTR-gtDATA ITEM, then
  • Set TEMP-gtLINK PTR -gt LINK, exit
  • else
  • TEMP PTR
  • PTR PTR -gt LINK
  • Stop

START
Node A
Node B
Node N
1000
2000
3000
4000
5000
3500
START
Node A
Node B
Node N
1000
2000
3000
4000
5000
3500
..
3500
PTR
ITEM
TEMP
18
  • Define struct
  • Function prototypes
  • Initialize variables

19
  • Input choice
  • switch statement

20
(No Transcript)
21
(No Transcript)
22
(No Transcript)
23
Enter your choice 1 to insert an element
into the list. 2 to delete an element from the
list. 3 to end. ? 1 Enter a character B The
list is B --gt NULL   ? 1 Enter a character
A The list is A --gt B --gt NULL   ? 1 Enter a
character C The list is A --gt B --gt C --gt
NULL   ? 2 Enter character to be deleted D D not
found.   ? 2 Enter character to be deleted B B
deleted. The list is A --gt C --gt NULL
Program Output
24
Trees
  • Definitions
  • Tree A tree is a collection of nodes. If the
    collection is not empty, it must consist of a
    unique root node, r, and zero or more nonempty
    subtrees T1, T2,, Tk, with roots connected by a
    direct edge from r
  • For a tree of N nodes, there must be N-1 edges.
  • A node with no child is called a leaf node nodes
    with the same parents are siblings.
  • Path a path from node n1 to nk is a sequence of
    nodes n1, n2, , nk such that ni is the parent of
    ni1 (1 ? i ? k) The no. of edges in the path is
    call the length of the path A length 0 path is a
    path from a node to itself. There is a unique
    path from root to any node ni The length of this
    path is called the depth of ni thus, the root is
    a depth 0 node.
  • The height of a node ni is the length of the
    longest path from ni to a leaf node, thus, the
    height of a leaf node is 0.
  • The height of a tree is the height of its root
    node. The depth of a tree is the depth of the
    deepest leaf node, which is the same as the
    height of the tree.

25
Tree Terminology (Review)
  • Height deepest level, not including root.
  • Leaf nodes have no children
  • Interior Notes have at least one child
  • Degree of a node Number of children
  • In a binary tree, interior nodes have degree 1 or
    2.

26
Binary Trees
  • Binary trees
  • Definition A binary tree is a tree in which no
    nodes can have more than two children.
  • The root node is the first node in a tree.
  • Each link in the root node refers to a child
  • A node with no children is called a leaf node
  • Total number of nodes in a full binary tree of
    height k is 2k-1
  • Implementation
  • Struct BinaryNode
  • data_type element
  • BinaryNode ?left
  • BinaryNode ?right

27
Binary Tree
  • Binary tree has interior nodes restricted to
    degree 1 or 2.

28
Array implementation of binary tree
  • We can embed the nodes of a binary tree into a
    one-dimensional array by defining a relationship
    between the position of each parent node and the
    position of its children.
  • 1. left_child of node i is 2i
  • 2. right_child of node i is 2i1
  • 3. parent of node i is i/2 (integer
    division)
  • How much space is required for the array to
    represent a tree of depth d?

2d1 1 (must assume full tree)
29
Dynamic Implementation of Binary Tree
  • A dynamic implementation of a binary tree uses
    space proportional to the actual number of nodes
    used. Not the size of the full tree.
  • Struct BinaryNode
  • data_type element
  • BinaryNode ?left
  • BinaryNode ?right

30
Binary Search Tree (BST)
  • Binary search tree (BST)
  • Values in left subtree less than parent
  • Values in right subtree greater than parent
  • Facilitates duplicate elimination
  • Fast searches - for a balanced tree, maximum of
    log2(n) comparisons
  • Construct a BST from the values 47, 25, 77, 11,
    43, 65, 93, 7, 17, 31, 44, 68

31
Binary Search Tree (BST)
  • Question Construct an Binary Search Tree (BST)
    for the data
  • 555 501 701 358 513 561 797 345 490 724

555
Array Implementation
32
Binary Trees
  • Tree traversals
  • In-order traversal prints the node values in
    ascending order (LNR)
  • 1. Traverse the left sub-tree with an in-order
    traversal
  • 2. Process the value in the node (i.e., print the
    node value)
  • 3. Traverse the right sub-tree with an in-order
    traversal
  • Pre-order traversal (NLR)
  • 1. Process the value in the node
  • 2. Traverse the left sub-tree with a preorder
    traversal
  • 3. Traverse the right sub-tree with a preorder
    traversal
  • Post-order traversal (LRN)
  • 1. Traverse the left sub-tree with a post-order
    traversal
  • 2. Traverse the right sub-tree with a post-order
    traversal
  • 3. Process the value in the node

33
Tree Traversal is Naturally recursive
  • Naturally recursive because
  • Subgraphs are similar in character to the
    original graph.
  • Subgraphs are smaller.
  • Guaranteed to eventually hit a leaf (acyclic is
    assumed)
  • Obeys fundamental rules of recursion
  • 1.) Base cases
  • 2.) Makes progress through recursion
  • 3.) Assume recursive calls work (abstraction)
  • Always specify the base case otherwise, infinite
    recursive will occur and cause stack-overflow
    error.

34
Sample recursive programs
Post-order (LRN) void postorder(tree_pointer
ptr) if (ptr) postorder(ptr-gtleft_chi
ld) postorder(ptr-gtright_child)
printf(d, ptr-gtdata)
  • In-order (LNR)
  • void inorder(tree_pointer ptr)
  • if (ptr)
  • inorder(ptr-gtleft_child)
  • printf(d, ptr-gtdata)
  • inorder(ptr-gtright_child)
  • Pre-order (NLR)
  • void preorder(tree_pointer ptr)
  • if (ptr)
  • printf(d, ptr-gtdata)
  • preorder(ptr-gtleft_child)
  • preorder(ptr-gtright_child)

35
Example Expression Tree
  • Example An Expression Tree
  • Perform in-order traversal (LNR)
  • A B C E / F
  • Perform post-order traversal (LRN)
  • A B C E F /
  • Perform pre-order traversal (NLR)
  • - A B C / E F

36
Tree Traversal Example
  • Try your hand at traversing this tree
  • In-order (LNR)
  • Pre-order (NLR)
  • Post-order (LRN)

Apr, Aug, Dec, Feb, Jan, Jun, Jan, Mar, May, Nov,
Oct, Sep
Mar, Apr, Jul, Feb, Dec, Aug, Jun, Jan, May, Sep,
Oct, Nov
Aug, Dec, Jan, Jun, Feb, Jul, Apr, Nov, Oct, Sep,
May, Mar
37
Stacks
  • Stack model
  • A stack is a list with the restriction that
    insertion deletion can be performed only at the
    end (or top) of the list.
  • Only the top node is accessible New nodes can be
    added and removed only at the top
  • Similar to a pile of dishes
  • Last-in, first-out (LIFO)
  • Bottom of stack indicated by a link member to
    NULL
  • Constrained version of a linked list
  • push
  • Adds a new node to the top of the stack
  • pop
  • Removes a node from the top
  • Stores the popped value
  • Returns true if pop was successful
  • A stack can be empty, pop from an empty stack
    is an error
  • A stack can never be full (assuming infinite
    memory)

38
Stack Operations
  • struct list
  • int data
  • struct list next
  • typedef struct list node
  • typedef node link
  • Push
  • link push(link stack, int value)
  • link newnode
  • newnode (link)malloc(sizeof(node))
  • newnode-gtdata value
  • newnode-gtnext stack
  • stack newnode
  • return(stack)
  • Pop

newnode
4
stack
4
newnode
stack
3
3
2
2
1
1
39
Queues
  • Queue
  • Similar to a supermarket checkout line
  • First-in, first-out (FIFO)
  • Nodes are removed only from the head
  • Nodes are inserted only at the tail
  • Insert and remove operations
  • Enqueue (insert) and dequeue (remove)
  • Queue Model
  • queue is a list, with insertion done only at one
    end and deletion done at the other end.
  • enqueue insert an element at the end of the
    queue
  • dequeue delete (and return) the element at the
    start of the queue
  • first in first out model (FIFO)
  • Linked list implementation of queues
  • operating as a list
  • constant time for enqueue dequeue (keeping
    pointer to both the head and tail of the list)

40
Queue Operations
  • enqueue
  • link enqueue(link queue, int value)
  • link newnode
  • newnode (link)malloc(sizeof(node))
  • newnode-gtdata value
  • newnode-gtnext NULL
  • if (queue!NULL)
  • queue-gtnext newnode
  • queue queue-gtnext
  • else
  • queue queue-gtnext
  • return(queue)
  • dequeue
  • link dequeue(link queue, int valuePtr)

newnode
queue
Write a Comment
User Comments (0)
About PowerShow.com