|
| 1 | +""" |
| 2 | +Hey, we are going to find an exciting number called Catalan number which is use to find |
| 3 | +the number of possible binary search trees from tree of a given number of nodes. |
| 4 | +
|
| 5 | +We will use the formula: t(n) = SUMMATION(i = 1 to n)t(i-1)t(n-i) |
| 6 | +
|
| 7 | +Further details at Wikipedia: https://en.wikipedia.org/wiki/Catalan_number |
| 8 | +""" |
| 9 | +""" |
| 10 | +Our Contribution: |
| 11 | +Basically we Create the 2 function: |
| 12 | + 1. catalan_number(node_count: int) -> int |
| 13 | + Returns the number of possible binary search trees for n nodes. |
| 14 | + 2. binary_tree_count(node_count: int) -> int |
| 15 | + Returns the number of possible binary trees for n nodes. |
| 16 | +""" |
| 17 | + |
| 18 | + |
| 19 | +def binomial_coefficient(n: int, k: int) -> int: |
| 20 | + """ |
| 21 | + Since Here we Find the Binomial Coefficient: |
| 22 | + https://en.wikipedia.org/wiki/Binomial_coefficient |
| 23 | + C(n,k) = n! / k!(n-k)! |
| 24 | + :param n: 2 times of Number of nodes |
| 25 | + :param k: Number of nodes |
| 26 | + :return: Integer Value |
| 27 | +
|
| 28 | + >>> binomial_coefficient(4, 2) |
| 29 | + 6 |
| 30 | + """ |
| 31 | + result = 1 # To kept the Calculated Value |
| 32 | + # Since C(n, k) = C(n, n-k) |
| 33 | + if k > (n - k): |
| 34 | + k = n - k |
| 35 | + # Calculate C(n,k) |
| 36 | + for i in range(k): |
| 37 | + result *= n - i |
| 38 | + result //= i + 1 |
| 39 | + return result |
| 40 | + |
| 41 | + |
| 42 | +def catalan_number(node_count: int) -> int: |
| 43 | + """ |
| 44 | + We can find Catalan number many ways but here we use Binomial Coefficent because it |
| 45 | + does the job in O(n) |
| 46 | +
|
| 47 | + return the Catalan number of n using 2nCn/(n+1). |
| 48 | + :param n: number of nodes |
| 49 | + :return: Catalan number of n nodes |
| 50 | +
|
| 51 | + >>> catalan_number(5) |
| 52 | + 42 |
| 53 | + >>> catalan_number(6) |
| 54 | + 132 |
| 55 | + """ |
| 56 | + return binomial_coefficient(2 * node_count, node_count) // (node_count + 1) |
| 57 | + |
| 58 | + |
| 59 | +def factorial(n: int) -> int: |
| 60 | + """ |
| 61 | + Return the factorial of a number. |
| 62 | + :param n: Number to find the Factorial of. |
| 63 | + :return: Factorial of n. |
| 64 | +
|
| 65 | + >>> import math |
| 66 | + >>> all(factorial(i) == math.factorial(i) for i in range(10)) |
| 67 | + True |
| 68 | + >>> factorial(-5) # doctest: +ELLIPSIS |
| 69 | + Traceback (most recent call last): |
| 70 | + ... |
| 71 | + ValueError: factorial() not defined for negative values |
| 72 | + """ |
| 73 | + if n < 0: |
| 74 | + raise ValueError("factorial() not defined for negative values") |
| 75 | + result = 1 |
| 76 | + for i in range(1, n + 1): |
| 77 | + result *= i |
| 78 | + return result |
| 79 | + |
| 80 | + |
| 81 | +def binary_tree_count(node_count: int) -> int: |
| 82 | + """ |
| 83 | + Return the number of possible of binary trees. |
| 84 | + :param n: number of nodes |
| 85 | + :return: Number of possilble binary trees |
| 86 | +
|
| 87 | + >>> binary_tree_count(5) |
| 88 | + 5040 |
| 89 | + >>> binary_tree_count(6) |
| 90 | + 95040 |
| 91 | + """ |
| 92 | + return catalan_number(node_count) * factorial(node_count) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + node_count = int(input("Enter the number of nodes: ").strip() or 0) |
| 97 | + if node_count <= 0: |
| 98 | + raise ValueError("We need some nodes to work with.") |
| 99 | + print( |
| 100 | + f"Given {node_count} nodes, there are {binary_tree_count(node_count)} " |
| 101 | + f"binary trees and {catalan_number(node_count)} binary search trees." |
| 102 | + ) |
0 commit comments