Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Data Structures Dale Roberts, Lecturer Computer Science,

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Data Structures Dale Roberts, Lecturer Computer Science,"— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Data Structures Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

2 Dale Roberts 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 Dale Roberts 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 Dale Roberts 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? 345 358 490 501 513 724 797 701 561 555 345 358 490 501 513 724 797 701 561 555 498 345 358 490 501 513 724 797 701 561 555 498

5 Dale Roberts Deletion of an Element from a List Algorithm: 1. locate the element in the list (this involves searching) 2. delete the element 3. 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: 1. Locate 358:if we use ‘linear search’, we’ll compare 358 with each element of the list starting from the location 0. 2. 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 1. 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 Dale Roberts Insertion of an Element in List Algorithm: 1. 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) 2. reorganize the list and create an ‘empty’ slot 3. 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: 1. Locate the appropriate position by performing a binary search. 505 should be stored in location 4. 2. 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 location: 0 1 2 3 4 5 6 7 8 9 10 3. 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 Dale Roberts 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 Dale Roberts 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 list’s 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 Dale Roberts 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 3005 5000 1004 2001 4002 1 2 3 4 5 3 NULL

10 Dale Roberts 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 100 NULL pointer (points to nothing)Data member and pointer 500 … 32

11 Dale Roberts 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 Dale Roberts 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 1. set PTR = START 2. repeat step 3 and 4 while PTR  NULL 3. print PTR->DATA 4. set PTR = PTR -> LINK 5. stop 1000 START 2000 30004000 PTR PTR = LINK[PTR] 10203040 1020304050 DataLink

13 Dale Roberts /* 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)); ptr=head; printf(“input 5 different data\n”); /* construct a linked list */ for (i=0; i<5; i++) { scanf(“%d”,&num); ptr->data = num; ptr->next=(link)malloc(sizeof(node));ptr=ptr->next;} ptr=NULL; /* List Traversal */ printf(“sequential print of the list\n”); ptr=head; while (ptr->next != NULL) { printf(“The value is: %d\n”, ptr->data); ptr=ptr->next; } 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

14 Dale Roberts /* 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->next = NULL; ptr=tail; printf(“input 5 different data\n”); /* construct a linked list */ for (i=0; i<5; i++) { scanf(“%d”,&num); ptr->data = num; head =(link)malloc(sizeof(node)); head->next = ptr; ptr = head; } ptr=ptr->next; /* List Traversal */ printf(“reverse print of the list\n”); while (ptr->next != NULL) { printf(“The value is: %d\n”, ptr->data); ptr=ptr->next; } 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

15 Dale Roberts 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 LOC=NULL if search is unsuccessful. Algorithm 1. set PTR = START 2. repeat step 3 while PTR  NULL 3. if ITEM == PTR -> DATA, then 4. set LOC = PTR, and Exit 5. else 6. set PTR = PTR -> LINK 7. set LOC = NULL /*search unsuccessful */ 8. Stop 1000 START 200030004000 PTR PTR = LINK[PTR]

16 Dale Roberts 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 1. Set PTR = START 2. Repeat step 3 while PTR  NULL 3. If PTR == A, then 4. Set N->LINK = PTR -> LINK (or = B) 5. Set PTR->LINK = N 6. exit 7. else 8. Set PTR=PTR->LINK 9. If PTR == NULL insertion unsuccessful 10. Stop START 1000 2000300040005000 Node ANode B PTR START 1000 2000300040005000 Node ANode B 3500 Node N 3 cases: first node, last node, in-between node. ( ex: if ITEM = 500? if ITEM = 6000?)

17 Dale Roberts 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 1. Set PTR=START and TEMP = START 2. Repeat step 3 while PTR  NULL 3. If PTR->DATA == ITEM, then 4. Set TEMP->LINK = PTR -> LINK, exit 5. else 6. TEMP = PTR 7. PTR = PTR -> LINK 8. Stop 3 cases: first node, last node, in-between node. ( ex: if ITEM = 1000? if ITEM = 5000?) PTR START 1000 2000300040005000 Node ANode B 3500 Node N START 1000 2000300040005000 Node ANode B 3500 Node N 3500 ITEM ….. TEMP

18 Dale Roberts 1/* Fig. 12.3: fig12_03.c 2 Operating and maintaining a list */ 3#include 4#include 5 6struct listNode { /* self-referential structure */ 7 char data; 8 struct listNode *nextPtr; 9}; 10 11typedef struct listNode ListNode; 12typedef ListNode *ListNodePtr; 13 14void insert( ListNodePtr *, char ); 15char delete( ListNodePtr *, char ); 16int isEmpty( ListNodePtr ); 17void printList( ListNodePtr ); 18void instructions( void ); 19 20int main() 21{ 22 ListNodePtr startPtr = NULL; 23 int choice; 24 char item; 25 Define struct Function prototypes Initialize variables Abstract Data Type

19 Dale Roberts 29 30 while ( choice != 3 ) { 31 32 switch ( choice ) { 33 case 1: 34 printf( "Enter a character: " ); 35 scanf( "\n%c", &item ); 36 insert( &startPtr, item ); 37 printList( startPtr ); 38 break; 39 case 2: 40 if ( !isEmpty( startPtr ) ) { 41 printf( "Enter character to be deleted: " ); 42 scanf( "\n%c", &item ); 43 44 if ( delete( &startPtr, item ) ) { 45 printf( "%c deleted.\n", item ); 46 printList( startPtr ); 47 } 48 else 49 printf( "%c not found.\n\n", item ); 50 } 51 else 52 printf( "List is empty.\n\n" ); 53 54 break; 26 instructions(); /* display the menu */ 27 printf( "? " ); 28 scanf( "%d", &choice ); Input choice switch statement

20 Dale Roberts 60 61 printf( "? " ); 62 scanf( "%d", &choice ); 63 } 64 65 printf( "End of run.\n" ); 66 return 0; 67} 68 69/* Print the instructions */ 70void instructions( void ) 71{ 72 printf( "Enter your choice:\n" 73 " 1 to insert an element into the list.\n" 74 " 2 to delete an element from the list.\n" 75 " 3 to end.\n" ); 76} 77 55 default: 56 printf( "Invalid choice.\n\n" ); 57 instructions(); 58 break; 59 }

21 Dale Roberts 91 92 while ( currentPtr != NULL && value > currentPtr->data ) { 93 previousPtr = currentPtr; /* walk to... */ 94 currentPtr = currentPtr->nextPtr; /*... next node */ 95 } 96 97 if ( previousPtr == NULL ) { 98 newPtr->nextPtr = *sPtr; 99 *sPtr = newPtr; 100 } 101 else { 102 previousPtr->nextPtr = newPtr; 103 newPtr->nextPtr = currentPtr; 104 } 105 } 106 else 107 printf( "%c not inserted. No memory available.\n", value ); 108 } 109 78/* Insert a new value into the list in sorted order */ 79void insert( ListNodePtr *sPtr, char value ) 80{ 81 ListNodePtr newPtr, previousPtr, currentPtr; 82 83 newPtr = malloc( sizeof( ListNode ) ); 84 85 if ( newPtr != NULL ) { /* is space available */ 86 newPtr->data = value; 87 newPtr->nextPtr = NULL; 88 89 previousPtr = NULL; 90 currentPtr = *sPtr;

22 Dale Roberts 133 free( tempPtr ); 134 return value; 135 } 136 } 137 138 return '\0'; 139 } 140 121 else { 122 previousPtr = *sPtr; 123 currentPtr = ( *sPtr )->nextPtr; 124 125 while ( currentPtr != NULL && currentPtr->data != value ) { 126 previousPtr = currentPtr; /* walk to... */ 127 currentPtr = currentPtr->nextPtr; /*... next node */ 128 } 129 130 if ( currentPtr != NULL ) { 131 tempPtr = currentPtr; 132 previousPtr->nextPtr = currentPtr->nextPtr; 110 /* Delete a list element */ 111 char delete( ListNodePtr *sPtr, char value ) 112 { 113 ListNodePtr previousPtr, currentPtr, tempPtr; 114 115 if ( value == ( *sPtr )->data ) { 116 tempPtr = *sPtr; 117 *sPtr = ( *sPtr )->nextPtr; /* de-thread the node */ 118 free( tempPtr ); /* free the de-threaded node */ 119 return value; 120 }

23 Dale Roberts 154 155 while ( currentPtr != NULL ) { 156 printf( "%c --> ", currentPtr->data ); 157 currentPtr = currentPtr->nextPtr; 158 } 159 160 printf( "NULL\n\n" ); 161 } 162} 151 printf( "List is empty.\n\n" ); 152 else { 153 printf( "The list is:\n" ); 147 /* Print the list */ 148 void printList( ListNodePtr currentPtr ) 149 { 150 if ( currentPtr == NULL ) 141 /* Return 1 if the list is empty, 0 otherwise */ 142 int isEmpty( ListNodePtr sPtr ) 143 { 144 return sPtr == NULL; 145 } 146 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 --> NULL ? 1 Enter a character: A The list is: A --> B --> NULL ? 1 Enter a character: C The list is: A --> B --> C --> NULL ? 2 Enter character to be deleted: D D not found. ? 2 Enter character to be deleted: B B deleted. The list is: A --> C --> NULL Program Output:

24 Dale Roberts 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 T 1, T 2,…, T k, 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. A E C D G F I H B M L K J N P Q –Path: a path from node n 1 to n k is a sequence of nodes n 1, n 2, …, n k such that n i is the parent of n i+1 (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 n i ; The length of this path is called the depth of n i ; thus, the root is a depth 0 node. –The height of a node n i is the length of the longest path from n i 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 Dale Roberts 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 Dale Roberts 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 2 k -1 –Implementation Struct BinaryNode { data_type element; BinaryNode  left; BinaryNode  right; } Binary Trees B A D C

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

28 Dale Roberts 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 2*i 1. left_child of node i is 2*i 2. right_child of node i is 2*i+1 2. right_child of node i is 2*i+1 3. parent of node i is i/2 (integer division) 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? 2 d+1 – 1 (must assume full tree)

29 Dale Roberts 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 Dale Roberts 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 log 2 (n) comparisons Construct a BST from the values: 47, 25, 77, 11, 43, 65, 93, 7, 17, 31, 44, 68 Binary Search Tree (BST) 47 25 77 11 43 65 93 68 7 17 31 44

31 Dale Roberts 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 501 555 501701 555 501701 358 555 501701 358513 555 501701 358513561 555 501701 358513561797 555 501701 358513561797 345490345 555 501701 358513561797 490724 555501701358513561797345490724 Array Implementation:

32 Dale Roberts 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 Dale Roberts Tree Traversal is “Naturally” recursive Naturally recursive because: 1. Subgraphs are similar in character to the original graph. 2. Subgraphs are smaller. 3. 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 Dale Roberts Sample recursive programs In-order: (LNR) void inorder(tree_pointer ptr) { if (ptr) if (ptr) { inorder(ptr->left_child); inorder(ptr->left_child); printf(“%d”, ptr->data); printf(“%d”, ptr->data); inorder(ptr->right_child); inorder(ptr->right_child); }} Pre-order: (NLR) void preorder(tree_pointer ptr) { if (ptr) if (ptr) { printf(“%d”, ptr->data); printf(“%d”, ptr->data); preorder(ptr->left_child); preorder(ptr->left_child); preorder(ptr->right_child); preorder(ptr->right_child); }} Post-order: (LRN) void postorder(tree_pointer ptr) { if (ptr) if (ptr) { postorder(ptr->left_child); postorder(ptr->left_child); postorder(ptr->right_child); postorder(ptr->right_child); printf(“%d”, ptr->data); printf(“%d”, ptr->data); }}

35 Dale Roberts Example: Expression Tree Example: An Expression Tree 1. Perform in-order traversal (LNR) A – B + C * E / F 2. Perform post-order traversal (LRN) A B – C E F / * + 3. Perform pre-order traversal (NLR) + - A B * C / E F + -* AB C / E F

36 Dale Roberts 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 Dale Roberts 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) top S4 S3 S2 S1 top 4 1 3 6  1 3 6 Push (4)  Pop (4) 1 3 6 top Stacks

38 Dale Roberts 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->data = value; newnode->next = stack; stack = newnode; return(stack);}Pop link pop(link stack, int *valuePtr) { link top; top = stack; stack = stack->next; *valuePtr = top->data; free(top);return(stack);} stack top 2 3 1 4 stack top 2 3 1 4 stack newnode 2 3 1 4stack newnode 2 3 1 4 valuePtr 4

39 Dale Roberts 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 Dale Roberts Queue Operations enqueue link enqueue(link queue, int value) { link newnode; newnode = (link)malloc(sizeof(node)); newnode->data = value; newnode->next = NULL; if (queue!=NULL) { queue->next = newnode; queue = queue->next; }else return(queue);}dequeue link dequeue(link queue, int *valuePtr) { link dequeuenode; dequeuenode = queue; *valuePtr = dequeuenode ->data; queue = queue->next; free(dequeuenode);return(queue);} newnodequeue


Download ppt "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Data Structures Dale Roberts, Lecturer Computer Science,"

Similar presentations


Ads by Google