CS3240: Data Structures and Algorithms

Questions: Heaps

 

1) What is the running time in terms of O notation for Heap-Insert?

O(lg n), where n is the number of items currently in the heap

2) Create the pseudo code to implement the function Extract-Max procedure given you can use the function Heapify.

suppose we have the heap

now we extract the node (root) which represents the maximum value and now we get

We now have to make this back into a heap. We can almost run the heapify procedure on the whole thing, except that we have two complete binary trees, not one.

 

SOLUTION:

move lowest node that is farthest to the right into the root position.

You get

NOW invoke Heapify:
>>>

The final result

 

© Lynne Grewe