• retagged by
42,487 views
125 125 votes

Let $T(n)$ be the number of different binary search trees on $n$ distinct elements.

Then $T(n) = \sum_{k=1}^{n} T(k-1)T(x)$, where $x$ is 

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

9 Answers

Best answer
107 107 votes

The summation is for each node, if that node happens to be the root. When a node is root, it will have $(k-1)$ nodes on the left sub tree ($k$ being any number) and correspondingly $(n-k)$ elements on the right sub tree.  So, we can write recurrence $T(k-1) * T(n-k) $ for the number of distinct binary search trees, as the numbers on left and right sub trees form BSTs independent of each other and only a difference in one of the sub trees produces a difference in the tree. Hence, answer is B. 

Knowing the direct formula can also help in getting the answer but is not recommended.

https://gatecse.in/number-of-binary-trees-possible-with-n-nodes/

• edited by
190 190 votes
left subtree + root + right subtree = total node
(k-1) + 1 + x = n
x = n - k
5 5 votes

Total number of nodes (n) = left subtree nodes + right subtree nodes(i.e x in the question) + 1(i.e for root)

                                       n = (k-1) + x + 1    respectively 

                                       x = n-k 

• edited by
3 3 votes

The answer should (a) n-k+1

Try to Solve T(n)=∑ T(k−1)T(n-k+1): 

T(0) = 0 (no of BST with zero node)

T(1) = 1 (no of BST with one nodes)

T(2) = 2   (no of BST with two nodes)

T(3) = 5  (no of BST with three nodes)

T(4) = 14 (no of BST with four nodes)

 

now verify using both options (a) and (b)

Option(a) : T(n)=∑ T(k−1)T(n-k+1): 

T(4) = T(0)T(4) + T(1)T(3) + T(2)T(2) + T(3)T(1)

        =  0 + 5 + 4 + 5

        = 14

which is correct.

 

now trying the option that is mentioned everywhere 

option (b) T(n)=∑ T(k−1)T(n-k):

 T(4) = T(0)T(3) + T(1)T(2) + T(2)T(1) + T(3)T(0)

         = 0 + 2 + 2 + 0

          = 4

which is wrong as no. of BST which 4 distinct keys = 14

So option (a) should be the answer.

 

   

3 3 votes

No. of the element in left subtree is k-1 

“-1” denote that root is not included as leftsub tree
if n is total no. of elements and no.of elements in left subtree is k-1


No. of  element in right subtree is 
total no. of element – no. element in left subtree – 1(excluding root)



n-(k-1)-1 =  n-k     (Ans)

Answer:
Position:
Show:

Related questions

61 61 votes
5 answers 5 answers
14.7k
14.7k views
Kathleen asked Sep 17, 2014
14,654 views
A program consists of two modules executed sequentially. Let $f_1(t)$ and $f_2(t)$ respectively denote the probability density functions of time taken to execute the two ...
94 94 votes
4 answers 4 answers
31.4k
31.4k views
Kathleen asked Sep 17, 2014
31,424 views
A data structure is required for storing a set of integers such that each of the following operations can be done in $O(\log n)$ time, where $n$ is the number of elements...
33 33 votes
3 answers 3 answers
29.5k
29.5k views
Kathleen asked Sep 16, 2014
29,459 views
Suppose the numbers $7, 5, 1, 8, 3, 6, 0, 9, 4, 2$ are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering o...
80 80 votes
10 answers 10 answers
31.8k
31.8k views
Kathleen asked Sep 17, 2014
31,804 views
Consider the function $f$ defined below.struct item { int data; struct item * next; }; int f(struct item *p) { return ((p == NULL) || (p->next == NULL)|| ((p->data <= p -...