• edited by
61,767 views
258 258 votes

When searching for the key value $60$ in a binary search tree, nodes containing the key values $10, 20, 40, 50, 70, 80, 90$ are traversed, not necessarily in the order given. How many different orders are possible in which these key values can occur on the search path from the root to the node containing the value $60$?

  1. $35$
  2. $64$
  3. $128$
  4. $5040$

18 Answers

Best answer
253 253 votes
$10, 20, 40, 50, 70,80, 90$

In BST search we if we go from say $10$ to $40$ while searching for $60$, we will never encounter $20$. So, $10, 20, 40$ and $50$ visited, means they are visited in order. Similarly, $90, 80$ and $70$ are visited in order. So, our required answer will be

$\frac{\text{No. of possible permutations of 7 numbers}}{\text{No. of possible permutations of numbers smaller than 60} \times \text{No. of possible permutations of numbers larger than 60}}$

(Since only one permutation is valid for both the smaller set of numbers as well as larger set of numbers)

$= \frac{7!}{4!3!}$

$=35$

Correct Answer: $A$
• edited by
90 90 votes

Question is similar to this question : https://gateoverflow.in/1275/gate2007_84-85

We will convert Moves to Text.
It is given that During Search we have Traversed these nodes

$$\large\{\color{blue}{10,20,40,50},\color{red}{70,80,90}\}$$

as it can be seen that Red ones are bigger than $60$ and blue ones are smaller than $60$.

Path to node $60$ has involved those nodes. So, one of the possible solution to the problem is $$\{L, L, L, L, S, S, S\}.$$ Any other solution will contains these moves only because at a time on a node we can get directions as S(meaning $60$ is smaller) or L(meaning $60$ is larger) on comparison and since it is given that those nodes were encountered it means directions were picked from that set. 

Hence, total number of possible solutions = all Permutations of that set, which is given by $\frac{7!}{4! \times 3!} = 35$

answer = option A

• edited by
44 44 votes


In BST search we if we go from say 10 40 while searching for 60, we will never encounter 20 So, 10,20,40 and 50 visited, means they are visited in order. Similarly, 90,80 and 70 are visited in order.

It means that suppose we have 7 place now we have to select 4 places out of 7 for first 4 elements and we have to place them so , it can be done in $^7c_4$ =$\frac{7!}{3!4!}$ways

Possible arrangements can be

_10_ _ 20 40 50

Or 10_20_ 40_50

so we have to place in such a way that  they will follow the order 10,20,40,50 now for remaining elements 90 ,80,70 there is only one choice we can place them in one way  on 3 remaining places so,it will be $\frac{7!}{3!4!}$$^3c_3$ways but they should follow the order 90,80,70 

See--It can be placed on blank places as shown above 

90 10 80 70 20 40 50

Or 10 90 20 80 40 70 50

Answer -$\frac{7}{3!4!}$$^3c_3$=35

(For better understanding You can draw a tree in the order which I had follow)

• edited by
42 42 votes

Trying to explain it in the simplest way possible,

The thing we have to keep in mind here is that the elements before 60, i.e. {10,20,40,50} will always be in the same order and the elements after 60, ie {90,80,70} will always be in the same order, but the elements of the two sets can come between each other.

This means that the order  20, 40, 50, 90, 80, 70 is valid, but 10, 20, 90, 30, 40, 80, 70, 50 is also valid.

So imagine we have 7 places to be filled. _ _ _ _ _ _ _

We can chose the 4 elements to be filled in this 7 places in $_{4}^{7}\textrm{C}$ ways. (Not using permutation here as the order of the elements has to be maintained)

Now out of the remaining 3 places there is only choice for the elements of the second set because once the elements of the first set are in their respective places, the second set must appear in only one order: 70,80,90.

So total possible orders: $_{4}^{7}\textrm{C}$ *1 =35

• edited by
1 flag:
✌ Edit necessary (Jon Snoww “Second set should be in order 90,80,70”)
17 17 votes
10 20 40 50 shoyld appear in order similarly  90 80 70 shoyld appear in order

It is similar to grid problem of combinatorics

So 7c3=35 is ans
16 16 votes

The sequences 10, 20,40, 50 and 90, 80, 70 can then be shuffled together, as long as their sub-sequences keep intact. Thus we can have 10, 20, 40, 50, 90, 80, 70, but also 10, 20, 90, 30, 40, 80, 70, 50.

but 10, 20 ,80,50,90,40,70 its not the possible sequencm when you make BST for it will be ambiguous coz of 90.

In the final 7 positions I have to choose 3 positions for the larger numbers (and the remaining 4 for the smaller numbers). I can choose these in 7C3=35.(73) ways

Answer:
Position:
Show:

Related questions

52 52 votes
11 answers 11 answers
24.9k
24.9k views
Ishrat Jahan asked Oct 29, 2014
24,915 views
Suppose you are given an implementation of a queue of integers. The operations that can be performed on the queue are:$\text{isEmpty (Q)}$ — returns true if the queue is ...
104 104 votes
13 answers 13 answers
47.0k
47.0k views
Ishrat Jahan asked Oct 29, 2014
46,966 views
Consider a hash function that distributes keys uniformly. The hash table size is $20$. After hashing of how many keys will the probability that any new key hashed collide...
34 34 votes
2 answers 2 answers
11.5k
11.5k views
Ishrat Jahan asked Oct 29, 2014
11,485 views
Consider the following C program: #include <stdio.h #define EOF -1 void push (int); /* push the argument on the stack */ int pop (void); /* pop the top of the stack */ vo...
61 61 votes
5 answers 5 answers
35.5k
35.5k views
Ishrat Jahan asked Oct 30, 2014
35,497 views
Consider the $B^{+}$ tree in the adjoining figure, where each node has at most two keys and three links.Keys $K15$ and then $K25$ are inserted into this tree in that orde...