|
| 1 | +# http://www.geeksforgeeks.org/total-number-of-possible-binary-search-trees-with-n-keys/ |
| 2 | +# https://www.youtube.com/watch?v=RUB5ZPfKcnY |
| 3 | +# Catalan number |
| 4 | + |
| 5 | +def number_of_binary_trees(n: int): |
| 6 | + count = [0 for i in range(n + 1)] |
| 7 | + count[0] = count[1] = 1 |
| 8 | + |
| 9 | + for i in range(2, n + 1): |
| 10 | + for j in range(i): |
| 11 | + count[i] += count[j] * count[i - j - 1] |
| 12 | + |
| 13 | + return count.pop() |
| 14 | + |
| 15 | +# main |
| 16 | +if __name__ == "__main__": |
| 17 | + n = int(input()) |
| 18 | + |
| 19 | + result = number_of_binary_trees(n) |
| 20 | + print("Number of Binary Trees possible for", n, "nodes") |
| 21 | + print(result) |
| 22 | + |
| 23 | +""" |
| 24 | +Input Explanation : |
| 25 | + - number of nodes |
| 26 | +
|
| 27 | +Input : |
| 28 | +5 |
| 29 | +
|
| 30 | +Output : |
| 31 | +Number of Binary Trees possible for 5 nodes |
| 32 | +42 |
| 33 | +""" |
| 34 | + |
| 35 | +''' |
| 36 | +Total number of possible Binary Search Trees with n different keys = Catalan number Cn = (2n)!/(n+1)!*n! |
| 37 | +
|
| 38 | +For n = 0, 1, 2, 3, … values of Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, …. So are numbers of |
| 39 | +Binary Search Trees. |
| 40 | +
|
| 41 | +Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. |
| 42 | +
|
| 43 | +Below is code for n’th Catalan number taken from here. |
| 44 | +
|
| 45 | +// See http://www.geeksforgeeks.org/program-nth-catalan-number/ |
| 46 | +// for reference of below code. |
| 47 | + |
| 48 | +unsigned long int binomialCoeff(unsigned int n, unsigned int k) |
| 49 | +{ |
| 50 | + unsigned long int res = 1; |
| 51 | + |
| 52 | + // Since C(n, k) = C(n, n-k) |
| 53 | + if (k > n - k) |
| 54 | + k = n - k; |
| 55 | + |
| 56 | + // Calculate value of [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1] |
| 57 | + for (int i = 0; i < k; ++i) |
| 58 | + { |
| 59 | + res *= (n - i); |
| 60 | + res /= (i + 1); |
| 61 | + } |
| 62 | + |
| 63 | + return res; |
| 64 | +} |
| 65 | + |
| 66 | +// A Binomial coefficient based function to find nth catalan |
| 67 | +// number in O(n) time |
| 68 | +unsigned long int catalan(unsigned int n) |
| 69 | +{ |
| 70 | + // Calculate value of 2nCn |
| 71 | + unsigned long int c = binomialCoeff(2*n, n); |
| 72 | + |
| 73 | + // return 2nCn/(n+1) |
| 74 | + return c/(n+1); |
| 75 | +} |
| 76 | +Run on IDE |
| 77 | +Here is a systematic way to enumerate these BSTs. Consider all possible binary search trees with each element at the |
| 78 | +root. If there are n nodes, then for each choice of root node, there are n – 1 non-root nodes and these non-root nodes |
| 79 | +must be partitioned into those that are less than a chosen root and those that are greater than the chosen root. |
| 80 | +
|
| 81 | +Let’s say node i is chosen to be the root. Then there are i – 1 nodes smaller than i and n – i nodes bigger than i. For |
| 82 | +each of these two sets of nodes, there is a certain number of possible subtrees. |
| 83 | +
|
| 84 | +Let t(n) be the total number of BSTs with n nodes. The total number of BSTs with i at the root is t(i – 1) t(n – i). |
| 85 | +The two terms are multiplied together because the arrangements in the left and right subtrees are independent. That is, |
| 86 | +for each arrangement in the left tree and for each arrangement in the right tree, you get one BST with i at the root. |
| 87 | +
|
| 88 | +Summing over i gives the total number of binary search trees with n nodes. |
| 89 | +
|
| 90 | +
|
| 91 | +
|
| 92 | +The base case is t(0) = 1 and t(1) = 1, i.e. there is one empty BST and there is one BST with one node. |
| 93 | +
|
| 94 | +
|
| 95 | +''' |
0 commit comments