GATE / CS ARCHIVE

GATE CSE 2025 SET 1 / QUESTION 35

Computing the height of a binary heap

Data Structures · Heaps & Priority Queues · Full GATE CSE 2025 Set 1 paper

GATE 2025 Set 1, Q35 1 mark · NAT
Report issue

Heaps & Priority Queues

Question 35, GATE 2025: Height is the largest number of edges on a root-to-leaf path. A Min-Heap stores 32 keys. Under the standard binary-heap convention, what is its height?
Tap to enlarge
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.

Original paper, page 35 · Answer key