next up previous
Next: Make it a generic Up: Assignment 10 Previous: Assignment 10

Fix the performance issues

In the solution we developed to last week's homework, we have a performance problem, which is that insert needs to know the rank of every node in the tree, but calculating those ranks takes O(n) time.

A proper implementation of a Heap should be able to perform an insertion or deletion in time proportional to the height of the heap, which for a well-balanced tree is only O(log n) time.

Also, the remove algorithm does not guarantee that the resulting tree is still complete. As a result, it is possible that the tree will not be well-balances, in which case the height could be proportional to the number of nodes!

1.
Modify your solution so that insertion takes time proportional to the height of the Heap.

2.
Modify your solution so that the tree is always complete.



Allen Downey
1999-11-11