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$?
- $35$
- $64$
- $128$
- $5040$
My attempt:
This is previous question of GATE CS/IT. This question is explained somewhere as:
There are $4$ values that are smaller than $60$ and $3$ values are greater than $60$. We can rearrange in $4! = 24$ ways smaller values and rearrange $3! = 6$ ways.
Total number of different order are $= 7! /(4!*3!) = 35$ ways.
My argument is:
Same as : Number of binary search tree of height $6$
We can have $2^4$ number of sub-trees in left of root $60$ and $2^3$ number of sub-trees in right of root $60$ (in This case, both right subtree and left sub-tree will have skewed, that means linear). So, total number of such trees $= 2^4 \times 2^3 = 2^7 = 128$
I am asking, what is flaw in my argument? Can you explain it, please?
You are counting something totally different from what the problem is talking about. It appears that you are counting how many binary search trees there are that have $60$ at the top such that both branches below $60$ are linear. But that's not at all what the binary search tree in the problem looks like. Instead, $60$ is at the bottom, and the entire binary search tree is linear.
(Incidentally, your count is also wrong for what you are counting. For instance, there are only $2^2$ linear binary search trees on $\{60,70,80,90\}$ that start with $60$. There are $2^3$ linear binary search trees on this set total, but half of them start with $90$ instead of $60$.)