The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+28 votes
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$
asked in DS by Loyal (4.3k points)
edited by | 2.1k views

4 Answers

+31 votes
Best answer

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 .

answered by Veteran (24.4k points)
selected by
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.
very nice answer ...

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

+32 votes

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.  

answered by Boss (7k points)
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)
+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
answered by Veteran (44.5k points)
–2 votes
what would be the max size of X?

 will it be  ((2^n) - 1 )  ??
answered by (327 points)
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

33,646 questions
40,193 answers
114,176 comments
38,663 users