|
| 1 | +''' |
| 2 | +Hey, We Are Going to find Some Exciting Number Called Catalan Number which is use to find the Possible binary search |
| 3 | +tree and Binary Search Tree from the Number of Nodes. |
| 4 | +WE USE THE FORMULA: |
| 5 | +t(n) = SUMMATION(i = 1 to n)t(i-1)t(n-i) |
| 6 | +
|
| 7 | +You can get Further Detail from Wikipedia: |
| 8 | +https://en.wikipedia.org/wiki/Catalan_number |
| 9 | +''' |
| 10 | +''' |
| 11 | +Our Contribution: |
| 12 | +Basically we Create the 2 function: |
| 13 | + 1. CountBST(n node) |
| 14 | + CountBST Function Count the Number of Binary Search Tree Crated By n number of Node. |
| 15 | + INPUT: N number i.e Number of Nodes |
| 16 | + OUTPUT: Count i.e Number of Possible BST |
| 17 | + 1. CountBT(n node) |
| 18 | + CountBT Function Count the Number of Binary Tree Crated By n number of Node. |
| 19 | + INPUT: N number i.e Number of Nodes |
| 20 | + OUTPUT: Count i.e Number of Possible BT |
| 21 | + |
| 22 | + >> CountBST(5) |
| 23 | + >> 42 |
| 24 | + >> CountBST(6) |
| 25 | + >> 132 |
| 26 | + >> CountBT(5) |
| 27 | + >> 5040 |
| 28 | + >> CountBT(6) |
| 29 | + >> 95040 |
| 30 | + |
| 31 | + |
| 32 | + >>> For Count of Binary Search Tree We find the Catalan Number Using Binomial Coefficient. |
| 33 | + >>> For Count of Binary Tree We find the Factorial as well as Binomial Coefficient. Multiply both of them to get |
| 34 | + Number of Count of Binary Tree. |
| 35 | +''' |
| 36 | + |
| 37 | +def binomial_coefficient(n, k): |
| 38 | + ''' |
| 39 | + Since Here we Find the Binomial Coefficient: |
| 40 | + https://en.wikipedia.org/wiki/Binomial_coefficient |
| 41 | + C(n,k) = n! / k!(n-k)! |
| 42 | + :param n: 2 times of Number of Nodes |
| 43 | + :param k: Number of Nodes |
| 44 | + :return: Integer Value |
| 45 | + ''' |
| 46 | + result = 1 # To kept the Calculated Value |
| 47 | + # Since C(n, k) = C(n, n-k) |
| 48 | + if k > (n - k): |
| 49 | + k = n - k |
| 50 | + #Calculate C(n,k) |
| 51 | + for i in range(k): |
| 52 | + result *= (n - i) #result = result * (n - i) |
| 53 | + result //= (i + 1) # result = result // (i + 1) |
| 54 | + |
| 55 | + return result |
| 56 | + |
| 57 | +''' |
| 58 | +We can find Catalan Number in many Ways: |
| 59 | +But here we use Binomial Coefficent because it done the Job in O(n) |
| 60 | +''' |
| 61 | +def catalan(n): |
| 62 | + ''' |
| 63 | + This Function will Find the Nth Catalan Number. |
| 64 | + :param n: n number of Nodes |
| 65 | + :return: Nth catalan Number |
| 66 | + ''' |
| 67 | + value = binomial_coefficient(2 * n, n); |
| 68 | + # We have to Find the Catalan Number Using 2nCn/(n+1) |
| 69 | + catalan_number = value // (n + 1) |
| 70 | + return catalan_number # return nth catalan Number |
| 71 | + |
| 72 | +def CountBST(n): |
| 73 | + ''' |
| 74 | + This Function Count the Number of Possible Binary Search Tree from Given Number of Nodes. |
| 75 | + :param n: Number of Nodes |
| 76 | + :return: Number : Total Number of Binary Search Tree Formed By Nodes |
| 77 | + ''' |
| 78 | + count = catalan(n) #find the Catalan Number at N index |
| 79 | + return count #Return the Number of Count |
| 80 | + |
| 81 | +def factorial(n): |
| 82 | + ''' |
| 83 | + This Fuction use to Calculate The Factorial of N Number. |
| 84 | + :param n: Nth Value to find the Factorial |
| 85 | + :return: Factorial of Nth Value. |
| 86 | + ''' |
| 87 | + result = 1 #kept the Result |
| 88 | + #Calculate the Value upto 1 * 2... n |
| 89 | + for i in range(1, n+1): |
| 90 | + result *= i # result = result * i |
| 91 | + return result |
| 92 | + |
| 93 | +def CountBT(n): |
| 94 | + ''' |
| 95 | + This Function return the Count of Binary Tree. |
| 96 | + :param n: n Number of Node |
| 97 | + :return: Count of Binary Tree |
| 98 | + ''' |
| 99 | + catalan_value = catalan(n) #find the nth Catalan Number |
| 100 | + factorial_value = factorial(n) #find the factorial Value |
| 101 | + count = catalan_value * factorial_value #find the Count of Binary Tree |
| 102 | + return count #return the Count |
| 103 | +if __name__ == '__main__': |
| 104 | + n = int(input('Enter the Number of Node: ')) |
| 105 | + print('Total Number of Possible Binary Search Tree from ',n, ' Number of node ',CountBST(n)) |
| 106 | + print('Total Number of Possible Binary Search Tree from ',n, ' Number of node ' ,CountBT(n)) |
| 107 | + |
| 108 | + |
0 commit comments