Heap Height and Analysis

The questions in this section are about a priority queue implemented as a binary min-heap. The height of a tree is the number of edges on the longest path from the root to a leaf. The root is at level .

Solution

Levels through are full and hold nodes. Level holds between and nodes. So

Taking of gives . Taking of gives . So is about .

Solution

add fills the next open slot and remove drops the last node, so the tree is complete after every operation. A complete binary tree of nodes has height about , whatever the values are.

Solution

peek is : it reads the root. add is : finding the next open slot and swimming up each take at most one step per level, and the height is about . remove is : finding the last node and sinking down each take at most one step per level.

Solution

On a binary heap, operations at each cost . On an unsorted array, the removes each scan in , so the total is . On a sorted array, the adds each shift in , so the total is also .