Top Banner
Heaps
15

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.

Jan 03, 2016

Download

Documents

Allen Rose
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

Heaps

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

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?

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

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

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

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)!

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

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.

Page 6: 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.

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.

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

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)

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

• Step 1: represent the heap as an array

• Consider element at index i• Its children are at 2i and 2i+1

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

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…

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

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

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

Analyzing worst-case complexity

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

Analyzing worst-case complexity

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

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

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

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

Page 15: 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.