Total number of possible Binary Search Trees using Catalan Number

Given an integer N, the task is to count the number of possible Binary Search Trees with N keys.

Examples:

Input: N = 2
Output: 2
For N = 2, there are 2 unique BSTs
     1               2  
      \            /
       2         1

Input: N = 9
Output: 4862

Approach: The number of binary search trees that will be formed with N keys can be calculated by simply evaluating the corresponding number in Catalan Number series.
First few Catalan numbers for n = 0, 1, 2, 3, … are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, …



Catalan numbers satisfy the following recursive formula:
C_0=1 \ and \ C_n_+_1=\sum_{i=0}^{n}C_iC_n_-_i \ for \ n\geq 0;

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Function to return the count
// of unique BSTs with n keys
int uniqueBSTs(int n)
{
    int n1, n2, sum = 0;
  
    // Base cases
    if (n == 1 || n == 0)
        return 1;
  
    // Find the nth Catalan number
    for (int i = 1; i <= n; i++) {
  
        // Recursive calls
        n1 = uniqueBSTs(i - 1);
        n2 = uniqueBSTs(n - i);
        sum += n1 * n2;
    }
  
    // Return the nth Catalan number
    return sum;
}
  
// Driver code
int main()
{
    int n = 2;
  
    cout << uniqueBSTs(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG 
{
      
// Function to return the count
// of unique BSTs with n keys
static int uniqueBSTs(int n)
{
    int n1, n2, sum = 0;
  
    // Base cases
    if (n == 1 || n == 0)
        return 1;
  
    // Find the nth Catalan number
    for (int i = 1; i <= n; i++) 
    {
  
        // Recursive calls
        n1 = uniqueBSTs(i - 1);
        n2 = uniqueBSTs(n - i);
        sum += n1 * n2;
    }
  
    // Return the nth Catalan number
    return sum;
}
  
// Driver code
public static void main (String[] args)
{
  
    int n = 2;
    System.out.println (uniqueBSTs(n));
  
}
}
  
// This code is contributed by jit_t.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the count
# of unique BSTs with n keys
def uniqueBSTs(n):
  
    n1, n2, sum = 0, 0, 0
  
    # Base cases
    if (n == 1 or n == 0):
        return 1
  
    # Find the nth Catalan number
    for i in range(1, n + 1):
  
        # Recursive calls
        n1 = uniqueBSTs(i - 1)
        n2 = uniqueBSTs(n - i)
        sum += n1 * n2
  
    # Return the nth Catalan number
    return sum
  
# Driver code
n = 2
  
print(uniqueBSTs(n))
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the count
// of unique BSTs with n keys
static int uniqueBSTs(int n)
{
    int n1, n2, sum = 0;
  
    // Base cases
    if (n == 1 || n == 0)
        return 1;
  
    // Find the nth Catalan number
    for (int i = 1; i <= n; i++) 
    {
  
        // Recursive calls
        n1 = uniqueBSTs(i - 1);
        n2 = uniqueBSTs(n - i);
        sum += n1 * n2;
    }
  
    // Return the nth Catalan number
    return sum;
}
  
// Driver code
static public void Main ()
{
          
    int n = 2;
    Console.WriteLine(uniqueBSTs(n));
  
}
}
  
// This code is contributed by ajit.

chevron_right


Output:

2


My Personal Notes arrow_drop_up

I like to write Technical Articles on Data Structures and Algorithm in my leisure time

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t, mohit kumar 29