heaps. what is a heap? like a binary search tree, but less structure within each level. guarantees:...

15
Heaps

Upload: allen-rose

Post on 03-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

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

• 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 functionPrecondition: trees rooted at L and R are heapsPostcondition: 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)

I:3

L:7 R:5

Case 1: max = LNeed to fix…

I:7

L:3 R:5

Case 2: max = IHeap OK!

I:5

L:3 R:7

Case 3: max = RNeed to fix…

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

Analyzing worst-case complexity

Analyzing worst-case complexity

• <= 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)

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