Presentation is loading. Please wait.

Presentation is loading. Please wait.

Heaps. What is a heap? Like a binary search tree, but less structure within each level. Guarantees: – Parent better than child – That’s it! What does.

Similar presentations


Presentation on theme: "Heaps. What is a heap? Like a binary search tree, but less structure within each level. Guarantees: – Parent better than child – That’s it! What does."— Presentation transcript:

1 Heaps

2 What is a heap? Like a binary search tree, but less structure within each level. Guarantees: – Parent better than child – That’s it! What does better mean? What do we use heaps for?

3 What can we do with a heap Keep the things we are most interested in close to the top (and fast to access) For instance: suppose we have some data. – We want to prioritize it. – We want to keep the most important thing at the top, at all times. Min heap: priority 1 is more important than 100 Max heap: other way around

4 Abstract data type (ADT) We are going to use max-heaps to implement the priority queue ADT – (for us, priority 100 is more important than priority 1) A priority queue Q offers (at least) 2 operations: – Extract-max(Q): returns the highest priority element – Insert(Q, e): inserts e into Q (and maintain the heap-order property) Can do same stuff with BST… why use heaps?? – BST extract-max is O(depth); heap is O(log n)!

5 Max-heap order property Look at any node u, and its parent p. p.priority >= u.priority After we Insert or Extract-max, we make sure that we restore the max-heap order property.

6 Maintaining heap-ordering FYI: We can prove that the heap-order property is always satisfied, by induction (on the sequence of Inserts and Extract-max’es that happen). Initially, there are no nodes, so it is true. Consider an Insert or Extract-max. Suppose it is true before this operation. Prove it is true after. (2 cases; Insert or Extract-max) By the magic (wonder, beauty, etc.) of induction, that’s all you have to show.

7 Movie time Using a heap to get the largest 31 elements from a list (played from 1:39 on) Using a heap to get the largest 31 elements from a list Notice that, after inserting, we have to “percolate” the larger elements down That’s the rough idea of maintaining the heap- order property We do it a little differently. (Insert at the bottom, and then fixup the heap-ordering.) – (but we don’t care about that right now)

8 Step 1: represent the heap as an array Consider element at index i Its children are at 2i and 2i+1

9 Building a heap: a helper function Precondition: trees rooted at L and R are heaps Postcondition: tree rooted at I is a heap MaxHeapify(A,I): L = LEFT(I) R = RIGHT(I) If L A[I] then max = L else max = I If R A[max] then max = R If max is L or R then swap(A[I],A[max]) MaxHeapify(A,max) I:3 L:7R:5 Case 1: max = L Need to fix… I:7 L:3R:5 Case 2: max = I Heap OK! I:5 L:3R:7 Case 3: max = R Need to fix…

10 The main function BUILD-MAX-HEAP(A): for i = heap_size(A)/2 down to 1 MaxHeapify(A,i) What does this look like? – MaxHeapify animation MaxHeapify animation

11 Analyzing worst-case complexity

12 Analyzing worst-case complexity

13 <= 2^d nodes at depth d Node at depth d has height <= h-d Cost to “heapify” one node at depth d is <= c(h-d) – Don’t care about constant c Cost to heapify all nodes at depth d is <= 2^d(h-d)

14 So, cost to heapify all nodes over all depths is:

15


Download ppt "Heaps. What is a heap? Like a binary search tree, but less structure within each level. Guarantees: – Parent better than child – That’s it! What does."

Similar presentations


Ads by Google