Computing the height of a binary heap
Heaps & Priority Queues

Answer & solution
Official answer: 5
Approach: Use the complete-binary-tree shape of a binary heap; key order does not determine its height.
At depth d, a binary tree can contain at most 2d nodes. The completely filled levels from depth 0 through depth 4 contain 1+2+4+8+16=31 nodes. A 32nd node therefore cannot fit in those levels and must occupy depth 5.
A heap is complete: all levels except possibly the last are full, with the last filled from left to right. Hence 32 keys create one node at depth 5 and none deeper. The height, measured in edges, is 5.
Equivalently, an n-node complete binary tree has height floor(log₂ n), and floor(log₂32)=5. Counting occupied levels would give 6, but the question explicitly uses edges. The answer relies on the conventional binary meaning of Min-Heap; arbitrary heap-ordered trees need not have this shape.



