A sequence of non-negative numbers which commonly appears in combinatorial problems is known as Catalan Numbers. It often involves recursively defined objects.
Euler's Polygon Division Problem asks the question, "In how many ways can a regular n-gon be divided into n-2 triangles if different orientations are counted separately?"
In tree enumeration problems such as the one given above, the solution involves the Catalan Sequence.
The first few Catalan Numbers starting from 0 are:1, 1, 2, 5, 14, 42, 132, 429, 1430,...
Problem Explanation
In this problem, we must find the number of unique binary search trees that can be formed using a given number of nodes. In order to calculate the number of arrangements of nodes, we must choose the number of nodes in the left sub-tree and the number of nodes in the right sub-tree while considering each node as the root node.
Example
Consider n = 5. We can derive the following combinations:
Therefore, for n = 5 nodes, the total number of binary search trees can be represented as:
f[5] = f[0]*f[4] + f[1]*f[3] + f[2]*f[2] + f[3]*f[1] + f[4]*f[0]
The intuitive approach would be to use a bottom-up dynamic programming approach to construct unique binary search trees by using each node of the tree as a root node to derive different combinations.
The recursive formula to find the total number of combinations is:
Create a vector to store the solutions of each sub-problem.
Initialize dp[0] as 1 to account for the first solution with the root being set as 1.
For each node in the tree, compute the number of unique binary search trees that can be formed using this node as the root node, by applying the formula: dp[i] += dp[j-1] * dp[i-j].
Time Complexity: O(m*n)
Mathematical Approach
In this approach, we use the Nth Catalan Number formula to find the number of ways in which the nodes can be arranged to form unique binary search trees.
The reason for this can be attributed to the pattern that can be observed when we examine the number of nodes and the number of possible combinations that could be formed.
n = 1->c = 1
n = 2->c = 2
n = 3->c = 5
n = 4->c = 14
n = 5->c = 42
From the values of c that we have obtained, we can observe that the pattern is similar to that of the Catalan Numbers series as illustrated in the Prerequisite Knowledge section.
int numTrees(int n) {
vector<int> dp(n + 1, 0);
// When 1 is set as root,
// left subtree = 0 children, right subtree = dp[n - 1] children
// Set dp[0] = 1
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
// If k is set as the root node,
// left subtree = (k - 1) children
// right subtree = (n - k) children
dp[i] += dp[j - 1] * dp[i - j];
}
}
return dp[n];
}
Mathematical Approach
int numTrees(int n) {
long long count = 1;
for(int i = n + 1; i <= 2 * n; i++) {
count = count * i / (i - n);
}
return count / (n + 1);
}