Number of Unique BST with a given key | Dynamic Programming
Given N , The task is to find the total number of unique BSTs that can be made using values from 1 to N .
Examples:
Input : N = 3 Output : 5Explanation: For N = 3, preorder traversal of Unique BSTs are: 1 2 3 1 3 2 2 1 3 3 1 2 3 2 1
Input : N = 4 Output : 14
Approach:
The solution is based on Dynamic Programming . For all possible values of i, consider i as root, then [1 . . . i-1] numbers will fall in the left subtree and [i+1 . . . N] numbers will fall in the right subtree.
The count of all possible BST’s will be count(N) = summation of (count(i-1)*count(N-i)) where i lies in the range [1, N] .
Follow the below steps to Implement the idea:
Create an array DP of size n+1
DP[0] = 1 and DP[1] = 1.
Run for loop from i = 2 to i <= n
Run a loop from j = 1 to j <= i
DP[i] = DP[i] + (DP[i – j] * DP[j – 1])
Return DP[n]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfBST(int n)
{
int dp[n + 1];
fill_n(dp, n + 1, 0);
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++) {
dp[i] = dp[i] + (dp[i - j] * dp[j - 1]);
}
}
return dp[n];
}
int main()
{
int N = 3;
cout << "Number of structurally Unique BST with " << N
<< " keys are : " << numberOfBST(N) << "\n";
return 0;
}
C
#include <stdio.h>
int dp[20];
int numberOfBST(int n)
{
if (n <= 1)
return 1;
if (dp[n])
return dp[n];
for (int i = 1; i <= n; i++)
dp[n] += numberOfBST(i - 1) * numberOfBST(n - i);
return dp[n];
}
int main()
{
int N = 3;
printf("Number of structurally Unique BST with %d keys "
"are : %d",
N, numberOfBST(N));
return 0;
}
Java
import java.io.*;
import java.util.Arrays;
class GFG {
public static int dp[] = new int[20];
static int numberOfBST(int n)
{
if (n <= 1)
return 1;
if (dp[n] > 0)
return dp[n];
for (int i = 1; i <= n; i++)
dp[n]
+= numberOfBST(i - 1) * numberOfBST(n - i);
return dp[n];
}
public static void main(String[] args)
{
int n = 3;
System.out.println("Number of structurally "
+ "Unique BST with " + n
+ " keys are : "
+ numberOfBST(n));
}
}
Python3
def numberOfBST(n):
dp = [0] * (n + 1)
dp[0], dp[1] = 1, 1
for i in range(2, n + 1):
for j in range(1, i + 1):
dp[i] = dp[i] + (dp[i - j] *
dp[j - 1])
return dp[n]
if __name__ == "__main__":
n = 3
print("Number of structurally Unique BST with",
n, "keys are :", numberOfBST(n))
C#
using System;
class GFG {
static int numberOfBST(int n)
{
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++) {
dp[i] = dp[i] + (dp[i - j] * dp[j - 1]);
}
}
return dp[n];
}
public static void Main()
{
int n = 3;
Console.Write("Number of structurally "
+ "Unique BST with " + n
+ " keys are : " + numberOfBST(n));
}
}
PHP
<?php
function numberOfBST($n)
{
$dp = array($n + 1);
for($i = 0; $i <= $n + 1; $i++)
$dp[$i] = 0;
$dp[0] = 1;
$dp[1] = 1;
for ($i = 2; $i <= $n; $i++)
{
for ($j = 1; $j <= $i; $j++)
{
$dp[$i] += (($dp[$i - $j]) *
($dp[$j - 1]));
}
}
return $dp[$n];
}
$n = 3;
echo "Number of structurally ".
"Unique BST with " ,
$n , " keys are : " ,
numberOfBST($n) ;
?>
Javascript
<script>
function numberOfBST(n){
let dp = new Array(n + 1).fill(0)
dp[0] = 1, dp[1] = 1
for(let i=2;i<n + 1;i++){
for(let j=1;j<i + 1;j++){
dp[i] = dp[i] + (dp[i - j] *
dp[j - 1])
}
}
return dp[n]
}
let n = 3
document.write("Number of structurally Unique BST with", n, "keys are :", numberOfBST(n))
</script>
Output
Number of structurally Unique BST with 3 keys are : 5
Time Complexity: O(N2 ) Auxiliary Space: O(N)
Last Updated :
28 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...