forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unique_BST.py
76 lines (51 loc) · 1.69 KB
/
Unique_BST.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Purpose: Total number of Unique BST's that can be
made using n keys/nodes.
Method: Dynamic Programming
Intution: Total number of Unique BST's with N nodes
= Catalan(N)
Here function Unique_BST() return the total number of
diffrent Binary Search Trees that can be made with N distinct nodes
Argument: N (number of distinct nodes)
return Type: int (Total number of binary tree)
Time Complexity: O(n)
Space Complexity: O(n)
Note: Since the number of possible binary search tree will be large
the answer is given in mod of 10^9+7
"""
# Catalan_Number (N) = ((2*N)!) / ((N+1)!*N!)
# Global Variables
MOD = 10**9+7
facto = [1] # Factorial Table
# To construct the factorial numbers using DP
def factorial(n):
global facto
for i in range(1, n+1):
facto += [(facto[-1]*i) % MOD]
# For Modular Inverse of num with respect to 10^9+7
def Mod_Inv(num):
return pow(num, MOD-2, MOD)
def Catalan_Number(num):
if num == 0 or num == 1:
return 1
# Constructing Factorial Table
factorial(2*num)
Numerator = facto[2*num]
Denominator = (facto[num+1]*facto[num]) % MOD
Catalan = (Numerator * Mod_Inv(Denominator)) % MOD
return Catalan
def Unique_BST(N):
# Nth Catalan Number
Cat = Catalan_Number(N)
return Cat
# ------------------------DRIVER CODE ------------------------
if __name__ == "__main__":
n = int(input("Enter the number of distinct nodes: "))
print("Total number of Binary Search Tree = ", Unique_BST(n))
"""
SAMPLE INPUT/OUTPUT
Enter the number of distinct nodes: 5
Total number of Binary Search Tree = 42
Enter the number of distinct nodes: 10
Total number of Binary Search Tree = 16796
"""