Skip to content
Official PYQ · 2006

GATE CS 2006 Q13 — Trees

Explain with AI

A scheme for storing binary trees in an array XX is as follows. Indexing of XX starts at 11 instead of 00. the root is stored at X[1]X[1]. For a node stored at X[i]X[i], the left child, if any, is stored in X[2i]X[2i] and the right child, if any, in X[2i+1]X[2i+1]. To be able to store any binary tree on n vertices the minimum size of XX should be

Choose one option
View worked solution

Answer: D

To store any arbitrary binary tree of nn vertices in the given array representation, we must size the array to accommodate the worst-case tree structure—the one that reaches the maximum possible index.

The maximum index is reached when the binary tree is completely right-skewed (i.e., every node has only a right child):

  • The root is at index i1=1i_1 = 1.
  • The right child of the root is at index i2=2(1)+1=3i_2 = 2(1) + 1 = 3.
  • The right child of that node is at index i3=2(3)+1=7i_3 = 2(3) + 1 = 7.

In general, the index iki_k of the kk-th node in a right-skewed path satisfies the recurrence relation:

ik=2⋅ik−1+1withi1=1i_k = 2 \cdot i_{k-1} + 1 \quad \text{with} \quad i_1 = 1

Solving this recurrence:

i1=21−1=1i2=22−1=3i3=23−1=7  ⋮in=2n−1\begin{aligned} i_1 &= 2^1 - 1 = 1 \\ i_2 &= 2^2 - 1 = 3 \\ i_3 &= 2^3 - 1 = 7 \\ &\ \ \vdots \\ i_n &= 2^n - 1 \end{aligned}

Thus, for a right-skewed binary tree with nn vertices, the last node is stored at index 2n−12^n - 1. To ensure we can store any such tree, the minimum size of the array XX must be at least 2n−12^n - 1.

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

Published
Report a correction
Keep exploringMore Trees questions