• retagged by
24,384 views
77 77 votes

The numbers $1, 2, .\dots n$ are inserted in a binary search tree in some order. In the resulting tree, the right subtree of the root contains $p$ nodes. The first number to be inserted in the tree must be

  1. $p$
  2. $p + 1$
  3. $n - p$
  4. $n - p + 1$

13 Answers

Best answer
95 95 votes

Option is C.

P: # elements in RST

$\Rightarrow$ Depending on $X,$ some number will go LST $\notin$ RST

$\Rightarrow$ 

Remaining elements for LST:  $n- (p+1)$

$\underbrace{1,2,\ldots,n-(p+1)}_{\text{LST}\\n-(p+1)\\ \text{elements}}\qquad \underbrace{n-p}_{\text{Root}\\1\\ \text{element}}\qquad \underbrace{(n-p)+1,\ldots,n}_{\text{RST}\\p\\ \text{elements}}$

• edited by
30 30 votes
from 1,...n elements p elements are on the right. so root or first inserted will be at n-p
13 13 votes

If the right subtree of the root contains p nodes then the first number to be inserted in the tree must be n-p

and If the left subtree of the root contains p nodes then the first number to be inserted in the tree must be p+1

9 9 votes

If total elements are n and Right Sub Tree has p elements 

then left subtree will have 

$n=root+elements in Right Subtree + Elements in  Left Subtree$

$n=1+p+Elements in  Left Subtree$

$Elements in  Left Subtree = n-p-1$

To gert $n-p-1$ elements is Left Subtree We must add root as $n-p-1+1 = n-p$

option (C)

8 8 votes

Let the numbers 1,2,3,4,5 are inserted in a binary search tree in ascending order.

The resulting tree, look like this:-

the right subtree of the root contains 4 nodes. The first number to be inserted in the tree must be
from option 
Answer will be C. 5-4 = 1

2 2 votes

We can solve this question by assuming some values like 

Suppose we have 1,2,3,4 nodes it means n =4 

1- If right subtree have 0 elements so p=0 put this value in option C which is n-p = 4-0 = 4 

so we have to insert 1st element is 4 in this case BST is left skew

2- If right subtree have 1 element so p=1 put value in option C so n-p= 3 

so 1st element is 3 in this case 4 is in right remaining in left 

3- If right subtree have 2 element so p=2 put value in option C so n-p= 2 

so 1st element is 2 in this case 3 and 4 will be in right remaining 1 in left

4- If right subtree have 3 element so p=3 put value in option C so n-p= 1 

so 1st element is 1 in this case 2,3,4 is in right it is right skew tree  

we can verify by this way in remaining options too but the correct option is C

Answer:
Position:
Show:

Related questions

34 34 votes
4 answers 4 answers
13.8k
13.8k views
Ishrat Jahan asked Nov 3, 2014
13,816 views
A binary search tree contains the numbers $1, 2, 3, 4, 5, 6, 7, 8.$ When the tree is traversed in pre-order and the values in each node printed out, the sequence of value...
39 39 votes
2 answers 2 answers
14.0k
14.0k views
Ishrat Jahan asked Nov 3, 2014
13,985 views
The following C function takes a singly-linked list of integers as a parameter and rearranges the elements of the list. The list is represented as pointer to a structure....
98 98 votes
8 answers 8 answers
38.1k
38.1k views
Ishrat Jahan asked Nov 3, 2014
38,118 views
In a binary tree, for every node the difference between the number of nodes in the left and right subtrees is at most $2$. If the height of the tree is $h 0$, then the m...
102 102 votes
2 answers 2 answers
30.9k
30.9k views
Ishrat Jahan asked Nov 3, 2014
30,935 views
A function $f$ defined on stacks of integers satisfies the following properties. $f(∅) = 0$ and $f (push (S, i)) = max (f(S), 0) + i$ for all stacks $S$ and integers $i$....