150 likes | 358 Views
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 better mean? What do we use heaps for?. What can we do with a heap.
E N D
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?
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
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)!
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.
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.
Movie time • Using a heap to get the largest 31 elements from a list (played from 1:39 on) • 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)
Step 1: represent the heap as an array • Consider element at index i • Its children are at 2i and 2i+1
Building a heap: a helper function I:3 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 <= heap_size(A) and A[L] > A[I] then max = L else max = I If R <= heap_size(A) and A[R] > A[max] then max = R If max is L or R then swap(A[I],A[max]) MaxHeapify(A,max) Case 1: max = L Need to fix… L:7 R:5 I:7 Case 2: max = I Heap OK! L:3 R:5 I:5 Case 3: max = R Need to fix… L:3 R:7
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
<= 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)