1 flag 2,075 views
5 5 votes

The number of insertion sequences of the numbers {1,2,3,4,5,6,7} which would lead to the following BST

How to tackle this kind of problem. Anyone!

  • 🚩 Edit necessary | 👮 Sanjana Ghosh | 💬 “No representation attached”

2 Answers

8 8 votes

Answer should be 80

 

4 should be the first element in any sequence without any ambiguity.

Now we have 6 slots where in all slots 2 should come before 1 and 3, and 6 should come before 5 and 7.

 

Now considering the positions of only 2, 1 and 3 in the insert sequence.

2 would have 4 places out of 6

Say 2 is in the 4th position

* * * 2 * *

 

Out of the positions indicated by ‘*’, only the last two positions are valid for 1 and 3

thus the total number of such combinations is 2P2. Where P means permutation and mPn means $\frac{m!}{(m – n)!}$

Now considering all the other combinations 

* * 2 * * *

Here we would have 3P2 combinations

And so on an so forth.

Thus the total number of sequences = 5P2 + 4P2 + 3P2 + 2P2 = 40

Now after filling up the three positions for 2, 1 and 3 we would have only 3 slots left 

There will be a least index among the three slots which would be necessarily filled up by 6 and the remaining 2 slots would be filled by 5 and 7, thus giving 2P2 permutations.

 

Thus total number of sequences = (5P2 + 4P2 + 3P2 + 2P2) * 2P2 = 40 * 2 = 80

 

0 0 votes
Total ways = C(3 + 3, 3) × (ways of left) × (ways of right)
           = C(6, 3) × (left ways) × (right ways)
           = 20 × (left ways) × (right ways)
 

ways of left (2-root) = C(1+1, 1) × (ways of 1) × (ways of 3)
                      = C(2,1) × 1 × 1 = 2
 

ways of right (6-root) = C(1+1, 1) × (ways of 5) × (ways of 7)
                       = C(2,1) × 1 × 1 = 2
 

Total ways = 20 × 2 × 2 = 80
 
Position:
Show:

Related questions

1 1 vote
1 1 answer
4.5k
4.5k views
mitesh kumar asked Oct 28, 2018
4,480 views
Consider an empty binary search tree of height $-1.$We need to fill the following sequence of numbers in it $: 11, 12, 13, 14, 15, 16, 17.$The number of ways in which the...
1 1 vote
2 2 answers
2.0k
2.0k views
rahul sharma 5 asked Oct 6, 2017
2,003 views
What is the time complexity to delete the root node in right skew tree?I knew the three cases of BST deletion:- 0 child,one child,two child.But how can we handle this par...
2 2 votes
1 1 answer
981
981 views
rahul sharma 5 asked Oct 2, 2017
981 views
What is the worst case time complexity to construct unique BST froma:) Inorder and preordera:) Inorder and postorder
7 7 votes
1 answers 1 answer
1.9k
1.9k views
Pankaj Joshi asked Jan 16, 2017
1,882 views
In a binary search tree, the key with value $5$ was searched after traversing nodes with values $1, 3, 4, 6, 7, 8, 9$ not necessarily in this order. Lets $P$ is the proba...