2.1k views

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 | 2.1k views

Option 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 .

selected
Shandaar Bro @pc
what is the maximum size for worst case then?

here "center of the question" is the word "ANY BINARY TREE"

In gate exam there are limited time and too much pressure ,in those moments is it possible to think this type of great logic with accuracy?? anyway too good explanation.

I think it must be n,because when we asked to find minium it should be minimum of either best or worst case.

For ex: when we asked to find minimum number of comparisons in insertion sort  then it will be minium(0(n-1)) when we have best case input not for worst case which is 0(n2).

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
@Arjun Since question is asking for minimum size of X, so we should go with a complete binary tree. That way only space of n will be required.
minimum size for any binary tree, hence we need to consider all possibilities
why we can store n vertices in 2^n-1 array space why not n space

here n vertices means all vertices of the complete binary tree(included left child and right child of all nodes)

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
what would be the max size of X?

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