1 / 14

Chapter 10 Heaps

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:

krikor
Download Presentation

Chapter 10 Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10Heaps Anshuman Razdan Div of Computing Studies razdan@asu.eduhttp://i3dea.asu.edu/razdan

  2. 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

  3. 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

  4. 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

  5. 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

  6. Add method • Suppose we want to add to the following 45 35 23 27 22 4 21 19 5 CST 230 Razdan et al

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

More Related