• edited by
28,199 views
101 101 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$

5 Answers

Best answer
151 151 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
51 51 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
13 13 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:
Position:
Show:

Related questions

32 32 votes
2 answers 2 answers
12.6k
12.6k views
Arjun asked Nov 27, 2016
12,564 views
Statement for Linked Answer Questions 76 & 77:A $3$-ary max heap is like a binary max heap, but instead of $2$ children, nodes have $3$ children. A $3$-ary heap can be re...
41 41 votes
3 answers 3 answers
7.9k
7.9k views
Rucha Shelke asked Sep 26, 2014
7,933 views
Statement for Linked Answer Questions 76 & 77:A $3$-ary max heap is like a binary max heap, but instead of $2$ children, nodes have $3$ children. A $3$-ary heap can be re...
136 136 votes
12 answers 12 answers
49.1k
49.1k views
Rucha Shelke asked Sep 26, 2014
49,060 views
An implementation of a queue $Q$, using two stacks $S1$ and $S2$, is given below: void insert (Q, x) { push (S1, x); } void delete (Q) { if (stack-empty(S2)) then if (sta...
64 64 votes
6 answers 6 answers
30.8k
30.8k views
Rucha Shelke asked Sep 16, 2014
30,783 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)$