CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root –pre-order traverse left subtree –pre-order traverse right subtree
CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 2 Traversals cont... In-order Traversal –in-order traverse left subtree –process root –in-order traverse right subtree
CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 3 Traversals cont... Post-order Traversal –post-order traverse left subtree –post-order traverse right subtree –process root
Chapter 10 Heaps Anshuman Razdan Div of Computing Studies
CST 230 Razdan et al5 Heap Storage Rules The elements of a heap must come from a totally ordered set. A heap is a binary tree in which the following 2 rules apply: –The element contained by each node is >= the elements of that nodes’ children –The tree is a complete binary tree so that every level is full except the deepest. At the deepest level, all the nodes are as far left as possible
CST 230 Razdan et al6 Heap or not?
CST 230 Razdan et al7 Heap Implementation A heap could be implemented just using a Binary Tree. Since a heap is a COMPLETE binary tree, the array implementation is more efficient. Example: i’s children are: i’s parent is:
CST 230 Razdan et al8 Heap = Priority Queue Recall that a priority queue is a queue in which items can be inserted/removed in priority order. A heap is an efficient implementation of a priority queue (highest priority item is at the root of the tree)
CST 230 Razdan et al9 Add method Suppose we want to add to the following
CST 230 Razdan et al10 Pseudocode for Add 1.Place the new element at the 1 st available location 2.while( the element has priority > parent ) swap element with parent add 30 add 40 add 50
CST 230 Razdan et al11 Remove Method (Priority Q) In a priority queue, we want to remove the item with highest priority root of heap Similar to add except we need to “heapify” downward instead of upward.
CST 230 Razdan et al12 Pseudocode for Remove 1.Copy root to return variable 2.Move last element of array to index 0 (root) 3.while( element priority < 1 of its children ) swap element with highest-priority child 4.return value from step
CST 230 Razdan et al13 Complexity Analysis Heapifiy O(log n) Add to heap O(log n), avg is constant remove root from heap (priority q) remove any element from heap find specific element
CST 230 Razdan et al14 Heapsort arrange array elements to be sorted so that the elements are a heap swap first element with last element of array (now largest value is at end of array) pretend the array is 1 element smaller and “heapify” downward from root. swap root to end and repeat previous
CST 230 Razdan et al15 Pseudocode 1.Convert array of n elements into a heap 2.unsorted = n 3.while( unsorted > 1 ) unsorted-- swap data[0] with data[unsorted] reheapify downward
CST 230 Razdan et al16 convert array to heap heapsize = 1 for( i = 1; i < n; i++ ) –add data[i] to heap in data[0..heapsize-1] –heapsize
CST 230 Razdan et al17 Time complexity of Heapsort Time complexity to makeHeap Time complexity for heapify downward Number times we must heapify downward Complexity of Heapsort =