• recategorized by
40,028 views
50 50 votes

How many distinct binary search trees can be created out of $4$ distinct keys?

  1. $5$
  2. $14$
  3. $24$
  4. $42$

6 Answers

Best answer
59 59 votes

answer - (B)

number of distinct BSTs = $\frac{^{2n}C_n}{n+1}$ (or) =$\frac{(2n)!}{(n+1)!n!}$

For a given Binary tree structure, there can be only $1$ BST. Hence, no. of different BSTs with $n$ nodes will be equal to the no. of different binary tree structures possible for $n$ nodes assuming nodes are unlabeled.

For derivation:
http://gatecse.in/number-of-binary-trees-possible-with-n-nodes/

• edited by
9 9 votes
Number of binary trees with unlabeled $n-vertices $
$T(n) = T(0)\times T(n-1)+T(1)\times T(n-2)+ \space ... \space+  T(n-1)\times T(0)$

$T(0)=T(1)=1$

$T(4) = T(0)\times T(3) + T(1)\times T(2) + T(2)\times T(1) + T(3)\times T(0)   \rightarrow$ eqn #1

$T(2) = T(0)\times T(1) + T(1)\times T(0) = 2$

$T(3) = T(0)\times T(2) + T(1)\times T(1) + T(2)\times T(0) = 2+1+2 = 5$

Just put values in eqn #1

$T(4)=5+2+2+5=14$
• edited by
6 6 votes

Incase you forgot the formula,

Number of BST's with 2 keys (1,2) = 2

Number of BST's with 3 keys (1,2,3) = 2 + 2 + 1 = (Root is 3) + (Root is 1)  +  (Root is 2)  = 5

Number of BST's with 4 keys (1,2,3,4) = 5 + 5 + 2 + 2 = 14 

Number of BST's with 5 keys (1,2,3,4,5) = 14 + 14 + 5 + 5 + 2 * 2 = 42

Number of BST's with 5 keys (1,2,3,4,5) = Root 5 + Root 1 + Root 4 + Root 2 + Root 3 (2 trees with 2 keys on either side)

 

6 6 votes

In general the BST is of the form     

 i.e. 1 node at root, 2 its children, another 4 as its children and so on.

                      

Hence for root=   4C1

for the next two children= 3C2

 and the next= 1C1

also we will add the 2 skew trees

thus total BST=  4C1 * 3C2 * 1C1    +  2 = 14 trees

2 2 votes

Answer

The number of distinct binary search trees that can be created out of 4 distinct keys is 14 (Option B).


Explanation

The number of distinct Binary Search Trees (BSTs) that can be formed using $n$ distinct keys is equal to the number of structurally unique binary trees with $n$ nodes. This value is determined by the $n^{th}$ Catalan number, denoted as $C_n$.

1. The Logic Behind the Calculation

For any unlabeled binary tree structure with $n$ nodes, there is exactly one way to fill it with $n$ distinct keys to satisfy the BST property (because the keys must appear in sorted order during an Inorder traversal). Therefore:

Number of distinct BSTs with $n$ keys = Number of unique binary tree structures with $n$ nodes.

2. The Catalan Formula

The formula for the $n^{th}$ Catalan number is:

 

$$C_n = \frac{1}{n+1} \binom{2n}{n} = \frac{(2n)!}{(n+1)! n!}$$

3. Step-by-Step Calculation for $n=4$

To find the answer for 4 distinct keys:

  • Step 1: Calculate the Binomial Coefficient

     

    $$\binom{2n}{n} = \binom{8}{4} = \frac{8 \times 7 \times 6 \times 5}{4 \times 3 \times 2 \times 1} = 70$$

  • Step 2: Apply the divisor $(n+1)$

     

    $$C_4 = \frac{70}{4+1} = \frac{70}{5} = 14$$

 

Answer:
Position:
Show:

Related questions

201 201 votes
9 answers 9 answers
79.9k
79.9k views
Kathleen asked Sep 22, 2014
79,907 views
A $5$ stage pipelined CPU has the following sequence of stages:IF – instruction fetch from instruction memoryRD – Instruction decode and register readEX – Execute: ALU op...
38 38 votes
5 answers 5 answers
16.5k
16.5k views
Kathleen asked Sep 22, 2014
16,518 views
Postorder traversal of a given binary search tree, $T$ produces the following sequence of keys$10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29$Which one of the following se...
32 32 votes
3 answers 3 answers
15.6k
15.6k views
gatecse asked Sep 21, 2014
15,623 views
Let $f(x)$ be the continuous probability density function of a random variable $x$, the probability that $a < x \leq b$, is :$f(b-a)$$f(b) - f(a)$$\int\limits_a^b f(x) dx...
33 33 votes
9 answers 9 answers
31.0k
31.0k views
Kathleen asked Sep 22, 2014
31,030 views
In a complete $k$-ary tree, every internal node has exactly $k$ children. The number of leaves in such a tree with $n$ internal node is:$nk$$(n-1)k + 1$$n(k-1) +1$$n(k-1)...