1 / 15

Heaps - PowerPoint PPT Presentation

  • Uploaded on

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Heaps' - macon

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

What is a heap
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
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
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
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
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
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)


Building a heap a helper function
Building a heap: a helper function


Precondition: trees rooted at L and R are heaps

Postcondition: tree rooted at I is a heap




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



Case 1: max = L

Need to fix…




Case 2: max = I

Heap OK!




Case 3: max = R

Need to fix…



The main function
The main function


for i = heap_size(A)/2 down to 1


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