in DS edited by
17,061 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$
in DS edited by
17.1k views

1 comment

what is the answer in case of already balanced binary tree?
1
1

4 Answers

99 votes
99 votes
Best answer

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
by

4 Comments

@Gupta731 bro since it is mentioned "any" binary tree and asking about minimum array size of array then we have take all the type of binary tree possible and find out minimum array size it is taking for each.

3
3
edited by
At first glance answer appears to be 2n+1 but its true if the tree is left skewed. But scenario changes when you try for right skewed tree. At first I got confused. Now, its clear.
1
1
If in place of minimum, the question asked for maximum size then the answer would have been same?
0
0
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

4 Comments

@ABKUNDAN Since its the Array representation of Binary Tree which has the very drawback that unnecessary array space is wasted if the tree is any normal Binary tree (i.e worst case 2n-1) and not a  Complete Binary tree (where in CBT its actually n).

So in such cases (any normal Binary tree), we instead prefer Linked List Representation of Binary Tree which occupies less space comparatively.

Though random access is not possible with Linked List as traversing is done via pointers and Arrays permits the use of formulae  to fetch any node randomly, so Array leads the choice when we have complete Binary tree.

1
1
What if asked Maximum size how do we are going to do that
0
0
What if asked Maximum size how do we are going to do that we can get same right
0
0
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
1 vote
1 vote
what would be the max size of X?

 will it be  ((2^n) - 1 )  ??
Answer:

Related questions