Full Transcript

Tree Data Structures Trees Data Structures  Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent  Binary tree  Tree with 0–2 children per node Tree Binary Tree Trees  Terminology no parent  Leaf  no child  Interior  non-leaf  Height  distanc...

Tree Data Structures Trees Data Structures  Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent  Binary tree  Tree with 0–2 children per node Tree Binary Tree Trees  Terminology no parent  Leaf  no child  Interior  non-leaf  Height  distance from root to leaf  Root Root node Interior nodes Leaf nodes Height Binary Search Trees  Key property  Value at node Smaller values in left subtree  Larger values in right subtree   Example X>Y X < Z  X Y Z Binary Search Trees  left data right Examples 5 10 2 5 10 45 30 5 45 30 2 25 45 2 10 25 30 25 Binary search trees Not a binary search tree Iterative Search of Binary Tree Node *Find( Node *n, int key) { left data right while (n != NULL) { if (n->data == key) // Found it return n; if (n->data > key) // In left subtree n = n->left; else // In right subtree n = n->right; } return null; } Node * n = Find( root, 5); Example Binary Searches  Find ( root, 2 ) root 10 5 5 10 > 2, left 5 > 2, left 30 2 45 2 = 2, found 2 25 30 45 5 > 2, left 10 25 2 = 2, found Example Binary Searches  Find (root, 25 ) 10 5 5 10 < 25, right 30 > 25, left 30 2 45 25 = 25, found 2 25 5 < 25, right 45 > 25, left 30 > 25, left 30 45 10 < 25, right 25 = 25, found 10 25 Types of Binary Trees    Degenerate – only one child Complete (Full)– always two children Balanced – “mostly” two children  more formal definitions exist, above are intuitive ideas Degenerate binary tree Balanced binary tree Complete binary tree Binary Trees Properties  Degenerate  Height = O(n) for n nodes  Similar to linked list  Balanced  Height = O( log(n) ) for n nodes  Useful for searches Height = O(n) for n O(n)=3 Height = O( log(n) )=O(log(6))=2 Degenerate binary tree Balanced binary tree Binary Search Properties  Time of search  Proportional to height of tree  Balanced binary tree  O( log(n) ) time  Degenerate tree O( n ) time  Like searching linked list / unsorted array  Binary Search Tree Construction  How to build & maintain binary trees?  Insertion  Deletion  Maintain key property (invariant)  Smaller values in left subtree  Larger values in right subtree Binary Search Tree – Insertion  Algorithm 1. 2. 3. 4.  Perform search for value X Search will end at node Y (if X not in tree) If X < Y, insert new leaf X as new left subtree for Y If X > Y, insert new leaf X as new right subtree for Y Observations   O( log(n) ) operation for balanced tree Insertions may unbalance tree Example Insertion  Insert ( 20 ) 10 5 2 30 > 20, left 30 25 20 10 < 20, right 25 > 20, left 45 Insert 20 on left Binary Search Tree – Deletion  Algorithm 1. 2. 3. Perform search for value X If X is a leaf, delete X Else // must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree Observation   O( log(n) ) operation for balanced tree Deletions may unbalance tree Example Deletion (Leaf)  Delete ( 25 ) 10 5 10 10 < 25, right 30 > 25, left 30 5 30 25 = 25, delete 2 25 45 2 45 Example Deletion (Internal Node)  Delete ( 10 ) 10 5 2 5 30 25 5 45 Replacing 10 with largest value in left subtree 2 5 30 25 2 45 Replacing 5 with largest value in left subtree 2 30 25 45 Deleting leaf Example Deletion (Internal Node)  Delete ( 10 ) 10 5 2 25 30 25 5 45 Replacing 10 with smallest value in right subtree 2 25 30 25 5 45 Deleting leaf 2 30 45 Resulting tree Balanced Search Trees  Kinds of balanced binary search trees  height balanced vs. weight balanced  “Tree rotations” used to maintain balance on insert/delete  Non-binary search trees  2/3 trees    each internal node has 2 or 3 children all leaves at same depth (height balanced) B-trees   Generalization of 2/3 trees Each internal node has between k/2 and k children   Each node has an array of pointers to children Widely used in databases Other (Non-Search) Trees  Parse trees  Convert from textual representation to tree representation  Textual program to tree  Used extensively in compilers  Tree  E.g. HTML data can be represented as a tree   representation of data called DOM (Document Object Model) tree XML Like HTML, but used to represent data  Tree structured  Parse Trees  Expressions, programs, etc can be represented by tree structures  E.g. Arithmetic Expression Tree  A-(C/5 * 2) + (D*5 % 4) + A % * / C 5 * 2 D 5 4 Preoder, Inorder, Postorder    In Preorder, the root is visited before (pre) the subtrees traversals In Inorder, the root is visited in-between left and right subtree traversal In Postorder, the root is visited after (pre) the subtrees traversals Preorder Traversal: 1. Visit the root 2. Traverse left subtree 3. Traverse right subtree Inorder Traversal: 1. Traverse left subtree 2. Visit the root 3. Traverse right subtree Postorder Traversal: 1. Traverse left subtree 2. Traverse right subtree 3. Visit the root Tree Traversal  Goal: visit every node of a tree  in-order traversal Void inOrder () { if (left != NULL) { cout << left->inOrder(); } cout << data << endl; if (right != NULL) right->inOrder() } + A % * / * 2 4 D 5 C 5 Output: A – C / 5 * 2 + D * 5 % 4 To disambiguate: print brackets Tree Traversal (contd.)  pre-order and post-order: Void preOrder () { cout << data << endl; if (left != NULL) left->preOrder (); if (right != NULL) right->preOrder (); } + A % * / * 2 C 5 Output: + - A * / C 5 2 % * D 5 4 void postOrder () { if (left != NULL) left->preOrder (); if (right != NULL) right->preOrder (); cout << data << endl; } Output: A C 5 / 2 * - D 5 * 4 % + D 5 4 More Illustrations for Traversals Assume: visiting a node is printing its label  Preorder: 1 3 5 4 6 7 8 9 10 11 12  Inorder: 4 5 6 3 1 8 7 9 11 10 12  Postorder: 4 6 5 3 8 11 12 10 9 7 1  1 3 7 5 4 8 9 10 6 11 12 More Illustrations for Traversals (Contd.) Assume: visiting a node is printing its data  Preorder: 15 8 2 6 3 7 11 10 12 14 20 27 22 30  Inorder: 2 3 6 7 8 10 11 12 14 15 20 22 27 30  Postorder: 3 7 6 2 10 14 12 11 8 22 30 27 20 15  15 20 8 2 6 10 12 3 27 11 7 22 30 14 Exercise Draw the binary search tree that would result from the insertion of the following integer keys: 14 58 18 10 99 52 33 69 74 17 Exercise Application of Traversal Sorting a BST     Observe the output of the inorder traversal of the BST example one slide earlier It is sorted This is no coincidence As a general rule, if you output the keys (data) of the nodes of a BST using inorder traversal, the data comes out sorted in increasing order Array Representation of Full Trees and Almost Complete Trees     A binary tree and almost complete binary trees, can be represented by an array A of the same length as the number of nodes A[k] is identified with node of label k That is, A[k] holds the data of node k Advantage: no need to store left and right pointers in the nodes  save memory  Direct access to nodes: to get to node k, access A[k]  Illustration of Array Representation 15 20 8 2 13 15 1   8 20 2 2 3 4 30 11 6 18 27 12 11 30 27 13 6 18 12 5 6 7 8 9 10 11 Notice: Left child of A[5] (of data 11) is A[2*5]=A[10] (of data 18), and its right child is A[2*5+1]=A[11] (of data 12). Parent of A[4] is A[4/2]=A[2], and parent of A[5]=A[5/2]=A[2] Priority Queues – Heaps 32 Recall Queues  FIFO: First-In, First-Out  Some contexts where this seems right?  Some contexts where some things should be allowed to skip ahead in the line? 33 Heap or Priority Queue that Allow Line Jumping  Need a new ADT  Operations: Insert an Item, Remove the “Best” Item 6 15 insert 2 23 18 12 45 3 7 deleteMin 34 Priority Queue ADT 1. PQueue data : collection of data with priority 2. PQueue operations   3. insert deleteMin PQueue property: for two elements in the queue, x and y, if x has a lower priority value than y, x will be deleted before y 35 Applications of the Priority Queue  Select print jobs in order of decreasing length  Forward packets on routers in order of urgency  Select most frequent symbols for compression  Sort numbers, picking minimum first  Anything greedy 36 Potential Implementations insert deleteMin Unsorted list (Array) O(1) O(n) Unsorted list (Linked-List) O(1) O(n) Sorted list (Array) O(n) O(1)* Sorted list (Linked-List) O(n) O(1) 37 Heap Properties 1. 2. Structure Property Ordering Property 38 Heap Structure Property  A binary heap is a complete binary tree. Complete binary tree – binary tree that is completely filled, with the possible exception of the bottom level, which is filled left to right. Examples: 39 Heap Type based on Heap Order Property  Min-Heap  Max-Heap Min-Heap  A heap (or min-heap to be precise) is an almost complete binary tree where  Every node holds a data value (or key)  The key of every node is ≤ the keys of the children Note: A max-heap has the same definition except that the Key of every node is >= the keys of the children Min-Heap Order Property Heap order property: For every X, the value in the parent of X is less than (or equal to) the value in X. 10 10 20 20 80 40 30 80 60 85 99 15 50 700 not a heap 42 Example of a Min-heap 5 20 8 15 33 16 18 30 11 12 27 Max-heap Therefore, the largest element (max-heap) in a heap is stored at the root, and the subtrees rooted at a node contain smaller values than does the node itself. Height of a Heap Operations on Heaps  Delete the minimum value and return it. This operation is called deleteMin.  Insert a new data value Applications of Heaps: • A heap implements a priority queue, which is a queue that orders entities not a on first-come first-serve basis, but on a priority basis: the item of highest priority is at the head, and the item of the lowest priority is at the tail • Another application: sorting, which will be seen later Heap Operations  findMin:  insert(val): percolate up.  deleteMin: percolate down. 10 20 40 50 700 80 60 85 99 65 50 DeleteMin in Min-heaps     The minimum value in a min-heap is at the root! To delete the min, you can’t just remove the data value of the root, because every node must hold a key Instead, take the last node from the heap, move its key to the root, and delete that last node But now, the tree is no longer a heap (still almost complete, but the root key value may no longer be ≤ the keys of its children Heap – Deletemin Basic Idea: 1. Remove root (that is always the min!) 2. Put “last” leaf node at root 3. Find smallest child of node 4. Swap node with its smallest child if needed. 5. Repeat steps 3 & 4 until no swaps needed. 52 DeleteMin: percolate down 10 20 40 50 15 60 700 85 99 65 65 20 40 50 15 60 85 99 700 53 Illustration of First Stage of deletemin 5 15 33 15 30 27 11 20 8 20 8 16 18 12 16 18 33 30 11 12 12 15 33 16 18 12 20 8 11 30 27 20 8 27 15 33 16 18 11 30 27 Restore Heap (Heapify) To bring the structure back to its “heapness”, we restore the heap  Swap the new root key with the smaller child.  Now the potential bug is at the one level down. If it is not already ≤ the keys of its   children, swap it with its smaller child Keep repeating the last step until the “bug” key becomes ≤ its children, or the it becomes a leaf Illustration of Restore-Heap 12 20 8 15 33 8 11 30 20 12 27 16 18 15 33 11 16 18 8 20 11 15 33 16 18 12 30 27 Now it is a correct heap 30 27 DeleteMin Code (Optimized) Object deleteMin() { assert(!isEmpty()); returnVal = Heap[1]; size--; newPos = percolateDown(1, Heap[size+1]); Heap[newPos] = Heap[size + 1]; return returnVal; } int percolateDown(int hole, Object val) { while (2*hole <= size) { left = 2*hole; right = left + 1; if (right ≤ size && Heap[right] < Heap[left]) target = right; else target = left; if (Heap[target] < val) { Heap[hole] = Heap[target]; hole = target; } else break; } return hole; } 57 Heap – Insert(val) Basic Idea: 1. Put val at “next” leaf position 2. Percolate up by repeatedly exchanging node until no longer needed 58 Insert: percolate up 10 20 80 40 50 60 700 65 85 99 15 10 15 40 50 700 59 80 20 65 85 60 99 Insert Code (optimized) void insert(int o) { if(!isFull()); size++; newPos = percolateUp(size,o); Heap[newPos] = o; } int percolateUp(int hole, int val) { while (hole > 1 && val < Heap[hole/2]) Heap[hole] = Heap[hole/2]; hole /= 2; } return hole; } 60 Heap Insertion  Insert 6 Heap Insertion  Add key in next available position Heap Insertion  Begin Unheap Heap Insertion Heap Insertion  Terminate unheap when  reach root  key child is greater than key parent Building a Heap 12 5 11 3 10 6 9 4 8 1 7 2 66 Building Heaps  What are the two properties of a heap?  Structure Property  Order Property  How do we work on heaps?  Fix the structure  Fix the order 67 BuildHeap: Floyd’s Method 12 5 11 3 10 6 9 4 8 1 7 2 Add elements arbitrarily to form a complete tree. Pretend it’s a heap and fix the heap-order property! 12 5 11 3 4 10 8 1 6 7 9 2 68 Buildheap pseudocode private void buildHeap() { for ( int i = currentSize/2; i > 0; i-- ) percolateDown( i ); } runtime: 69 BuildHeap: Floyd’s Method 12 5 11 3 4 10 8 1 6 7 9 2 70 BuildHeap: Floyd’s Method 12 5 11 3 4 10 8 1 2 7 6 71 9 BuildHeap: Floyd’s Method 12 12 5 11 3 4 10 8 1 2 7 6 5 9 11 3 4 1 8 10 2 7 9 6 72 BuildHeap: Floyd’s Method 12 12 5 11 3 4 10 8 1 5 2 7 9 3 12 6 4 5 1 6 73 4 8 1 8 2 3 10 7 11 11 9 10 2 7 6 9 BuildHeap: Floyd’s Method 12 12 5 11 3 4 10 8 1 2 7 5 9 11 3 6 4 1 8 10 2 7 6 12 12 5 2 3 4 1 8 10 6 7 9 11 1 9 2 3 4 5 8 10 6 7 11 9 74 Finally… 1 3 2 4 12 5 8 10 6 7 9 11 75 More on Heap Heaps (Ref. www.cse.unt.edu/~rada/CSCE3110/Lectures/Heaps.ppt )   A heap is a binary tree T that stores a keyelement pairs at its internal nodes It satisfies two properties:  MinHeap: key(parent) key(child)  [OR MaxHeap: key(parent) key(child)]  all levels are full, except 4 the last one, which is 5 6 left-filled 15 16 9 25 14 7 12 11 20 8 What are Heaps Useful for?  To implement priority queues  Priority queue = a queue where all elements have a “priority” associated with them  Remove in a priority queue removes the element with the smallest priority  insert  removeMin Heap or Not a Heap? Heap Properties  A heap T storing n keys has height h =  log(n + 1) , which is O(log n) 4 5 6 15 16 9 25 14 7 12 11 20 8 Heap Removal  Remove element from priority queues? removeMin( ) Heap Removal  Begin downheap Heap Removal Heap Removal Heap Removal  Terminate downheap when  reach leaf level  key parent is greater than key child Exercise Rearrange the heap after deleting the root. Exercise • Build the heap for the following items in the array.

Use Quizgecko on...
Browser
Browser