140 likes | 293 Views
Chapter 10 Heaps. Anshuman Razdan Div of Computing Studies razdan@asu.edu http://i3dea.asu.edu/razdan . 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:
E N D
Chapter 10Heaps Anshuman Razdan Div of Computing Studies razdan@asu.eduhttp://i3dea.asu.edu/razdan
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 al
Heap or not? 91 52 91 77 46 77 46 77 46 11 69 69 3 11 3 69 11 3 CST 230 Razdan et al
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 al
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 al
Add method • Suppose we want to add to the following 45 35 23 27 22 4 21 19 5 CST 230 Razdan et al
Pseudocode for Add • Place the new element at the 1st available location • while( the element has priority > parent ) • swap element with parent add 30 add 40 add 50 CST 230 Razdan et al
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 al
Pseudocode for Remove • Copy root to return variable • Move last element of array to index 0 (root) • while( element priority < 1 of its children ) • swap element with highest-priority child • 4.return value from step 1 CST 230 Razdan et al
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 al
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 al
Pseudocode • Convert array of n elements into a heap • unsorted = n • while( unsorted > 1 ) • unsorted-- • swap data[0] with data[unsorted] • reheapify downward CST 230 Razdan et al
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 al
Time complexity of Heapsort • Time complexity to makeHeap • Time complexity for heapify downward • Number times we must heapify downward • Complexity of Heapsort = CST 230 Razdan et al