3.4k 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$. 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 | 3.4k views
0

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 = A$ $X = B$ $X = C$ $X = A$ $X = B$ $X = C$ $X = A$ $X = B$ $X = C$ $n$ $2^{n - 1}$ $2^n-1$ Minimum size Best Case binary tree Minimum size Worst Case binary tree
edited
+2
Shandaar Bro @pc
0
what is the maximum size for worst case then?
+3

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

0
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.
0
+1

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

0
maza aa gaya @pc sir
0
@vidya swarupa

Some of the things you should know prior to the exam. Here that thing is " How binary tree is stored in array " .
0

maximum size should also be same as minimum size as here we have taken worst case binary tree which is already actually covering maximum size. Here in this question they have put "minimum" to just distract us....

0
why answer is not $n$. If $2^n-1$  is for minimum size then what would be the answer for the maximum size.

For maximum size, we consider right skewed tree and not for minimum size.

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
+3
@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.
+3
minimum size for any binary tree, hence we need to consider all possibilities
0
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)
+1

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

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 )  ??