Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10 Heaps Anshuman Razdan Div of Computing Studies

Similar presentations


Presentation on theme: "Chapter 10 Heaps Anshuman Razdan Div of Computing Studies"— Presentation transcript:

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

2 CST 230 Razdan et al2 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

3 CST 230 Razdan et al3 Heap or not? 91 7746 69 3 11 52 7746 69 3 11 91 7746 69 3 11

4 CST 230 Razdan et al4 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:

5 CST 230 Razdan et al5 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)

6 CST 230 Razdan et al6 Add method Suppose we want to add to the following 45 3523 27 21 22 19 5 4 4535232721224195

7 CST 230 Razdan et al7 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 4535232721224195 add 30 add 40 add 50

8 CST 230 Razdan et al8 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.

9 CST 230 Razdan et al9 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 1 454223273522419521

10 CST 230 Razdan et al10 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

11 CST 230 Razdan et al11 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

12 CST 230 Razdan et al12 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

13 CST 230 Razdan et al13 convert array to heap heapsize = 1 for( i = 1; i < n; i++ ) –add data[i] to heap in data[0..heapsize-1] –heapsize++ 213522272345421945

14 CST 230 Razdan et al14 Time complexity of Heapsort Time complexity to makeHeap Time complexity for heapify downward Number times we must heapify downward Complexity of Heapsort =


Download ppt "Chapter 10 Heaps Anshuman Razdan Div of Computing Studies"

Similar presentations


Ads by Google