edited by
17,201 views
67 votes
67 votes

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

  1. $\log_2 n$
  2. $n$
  3. $2n+1$
  4. $2^n-1$
edited by

4 Answers

Best answer
99 votes
99 votes

Answer is D.

To be able to store " any " binary tree on n vertices the minimum size of X should be

" Any Binary Tree and Size should be minimum " .
So We must consider worst case binary tree for this situation and find the minimum space required .

Minimum size for $\underline{\text{any}}$ binary tree

$\implies$Minimum size of worst case binary tree

$\qquad {X[i] = node \\ X[2i] = \text{Left child} \\ X[2i+1] = Right child}$

Let $n = 3$

 

 

 

 

 

 

$X[1] = A $
$X[2] = B $
$X[3] = C$
$X[1] = A$
$ X[2] = B$
$ X[4] = C$
$X[1] = A$
$  X[3] = B$
$  X[7] = C$
$n$ $2^{n - 1}$ $2^n-1$
Minimum size Best Case binary tree   Minimum size Worst Case binary tree
edited by
43 votes
43 votes

Answer should be (D).

Since binary tree can be of any form, the worst case happens for right skewed binary tree. Now, root goes to index $1$, its child goes to index $3$, its child goes to index $7$ and so on the nth vertex goes to $2^n - 1$ th index of array.  

edited by
11 votes
11 votes

For a right skewed binary tree, number of nodes will be 2^n – 1. For example, in below binary tree, node ‘A’ will be stored at index 1, ‘B’ at index 3, ‘C’ at index 7 and ‘D’ at index 15.

A
 \
   \
    B
      \
        \
         C
           \
             \
              D
Answer:

Related questions

44 votes
44 votes
5 answers
4
Rucha Shelke asked Sep 16, 2014
20,771 views
In a binary max heap containing $n$ numbers, the smallest element can be found in time $O(n)$ $O(\log n)$ $O(\log \log n)$ $O(1)$