Skip to content
Official PYQ · 2016

GATE CS 2016 Set 2 Q34 — Binary Heaps

Explain with AI
A complete binary min-heap is made by including each integer in [1,1023][1, 1023] exactly once. The depth of a node in the heap is the length of the path from the root of the heap to that node. Thus, the root is at depth 00. The maximum depth at which integer 99 can appear is _________.
View worked solution

Answer: 8

To find the maximum depth at which the integer 99 can appear in the min-heap, let us analyze the properties of a min-heap and the structure of the tree.

1. Tree Structure and Depths

The heap contains N=1023N = 1023 nodes. Since it is a complete binary tree, the depths of the nodes range from 00 (the root) to log⁡2(1023+1)−1=9\log_2(1023+1) - 1 = 9.

2. Min-Heap Property along a Path

In a min-heap, the value of any node is strictly less than the values of its children. Therefore, if we trace a path from the root to any node at depth dd, the values of the nodes along this path must be strictly increasing.

Let the path from the root to the node containing the integer 99 at depth dd be:

v0→v1→v2→⋯→vdv_0 \to v_1 \to v_2 \to \dots \to v_d

where v0v_0 is the root (depth 00) and vdv_d is the node containing 99 (depth dd). Due to the min-heap property:

val(v0)<val(v1)<val(v2)<⋯<val(vd)=9\text{val}(v_0) < \text{val}(v_1) < \text{val}(v_2) < \dots < \text{val}(v_d) = 9

3. Finding the Maximum Depth dd

Since the heap contains distinct integers from the range [1,1023][1, 1023], the smallest possible integers we can assign to the nodes along this path to minimize the value at depth dd are the smallest available positive integers:

  • val(v0)≥1\text{val}(v_0) \ge 1
  • val(v1)≥2\text{val}(v_1) \ge 2
  • val(v2)≥3\text{val}(v_2) \ge 3
  • …\dots
  • val(vd)≥d+1\text{val}(v_d) \ge d + 1

Since the node at depth dd contains the integer 99, we have:

9=val(vd)≥d+19 = \text{val}(v_d) \ge d + 1 d≤8d \le 8

4. Feasibility of Depth 8

We can easily construct such a heap where the path from the root to depth 88 is 1→2→3→4→5→6→7→8→91 \to 2 \to 3 \to 4 \to 5 \to 6 \to 7 \to 8 \to 9. The remaining 10141014 nodes in the tree can be filled with the remaining integers from [10,1023][10, 1023] without violating the min-heap property, as all remaining elements are strictly greater than 99.

Thus, the maximum depth at which the integer 99 can appear is 8.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Binary Heaps questions