Total number of BSTs using array elements
Last Updated :
30 Jun, 2022
Prerequisite: Total number of possible Binary Search Trees with n keys
Given an array arr[] of N integers. The task is to count the number of Binary Search Tree can be made using each node of element in arr[] as a root node.
Examples:
Input: arr[] = { 20, 10, 30 }
Output: 1 2 2
Input: arr[] = { 1, 2, 3, 4, 5 }
Output: 14 5 4 5 14
Approach:
The total number of possible Binary Search Tree(BST) is given by Catalan Number:
Cn = (2n)!/(( n+1)!*n!)
where n = number of distinct keys.
- Count the number of element(say c1) less than the current node.
- Count the number of element(say c2) greater than the current node.
- Then total number of Binary Search Tree(BST) can be formed using current element as a root node is equals to the product of total number of BST formed using c1 elements and total number of BST formed using c2 elements.
Total Number of BST = Cc1*Cc2
-
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int fact( int num)
{
int fact = 1;
while (num > 1)
{
fact *= num;
num -= 1;
}
return fact;
}
int catalan( int n)
{
return fact(2 * n)/(fact(n) * fact(n + 1)) ;
}
int main()
{
int n = 5;
int arr[] = {1, 2, 3, 4, 5};
int i,k;
for (k = 0; k < n; k++)
{
int s = 0;
for (i = 0; i < n; i++)
{
if (arr[i] < arr[k])
s += 1 ;
}
int catalan_leftBST = catalan(s) ;
int catalan_rightBST = catalan(n - s - 1) ;
int totalBST = catalan_rightBST * catalan_leftBST ;
cout<< totalBST << " " ;
}
}
|
Java
public class GFG
{
static int fact( int num)
{
int fact = 1 ;
while (num > 1 )
{
fact *= num;
num -= 1 ;
}
return fact;
}
static int catalan( int n)
{
return fact( 2 * n)/(fact(n) * fact(n + 1 )) ;
}
public static void main (String[] args)
{
int n = 5 ;
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int i,k;
for (k = 0 ; k < n; k++)
{
int s = 0 ;
for (i = 0 ; i < n; i++)
{
if (arr[i] < arr[k])
s += 1 ;
}
int catalan_leftBST = catalan(s) ;
int catalan_rightBST = catalan(n - s - 1 ) ;
int totalBST = catalan_rightBST * catalan_leftBST ;
System.out.print(totalBST + " " ) ;
}
}
}
|
Python3
def fact(num):
fact = 1 ;
while (num> 1 ):
fact = fact * num;
num = num - 1 ;
return fact;
def catalan(n):
return fact( 2 * n) / / (fact(n) * fact(n + 1 ))
if __name__ = = '__main__' :
n = 5
arr = [ 1 , 2 , 3 , 4 , 5 ]
for k in range (n):
s = 0
for i in range (n):
if arr[i] < arr[k]:
s + = 1
catalan_leftBST = catalan(s)
catalan_rightBST = catalan(n - s - 1 )
totalBST = catalan_rightBST * catalan_leftBST
print (totalBST, end = " " )
|
C#
using System;
class GFG
{
static int fact( int num)
{
int fact = 1;
while (num > 1)
{
fact *= num;
num -= 1;
}
return fact;
}
static int catalan( int n)
{
return fact(2 * n)/(fact(n) * fact(n + 1)) ;
}
public static void Main(String[] args)
{
int n = 5;
int []arr = {1, 2, 3, 4, 5};
int i,k;
for (k = 0; k < n; k++)
{
int s = 0;
for (i = 0; i < n; i++)
{
if (arr[i] < arr[k])
s += 1 ;
}
int catalan_leftBST = catalan(s) ;
int catalan_rightBST = catalan(n - s - 1) ;
int totalBST = catalan_rightBST * catalan_leftBST ;
Console.Write(totalBST + " " ) ;
}
}
}
|
Javascript
<script>
function fact(num)
{
var fact = 1;
while (num > 1)
{
fact *= num;
num -= 1;
}
return fact;
}
function catalan(n)
{
return fact(2 * n)/(fact(n) * fact(n + 1)) ;
}
var n = 5;
var arr = [1, 2, 3, 4, 5] ;
var i,k;
for (k = 0; k < n; k++)
{
var s = 0;
for (i = 0; i < n; i++)
{
if (arr[i] < arr[k])
s += 1 ;
}
var catalan_leftBST = catalan(s) ;
var catalan_rightBST = catalan(n - s - 1) ;
var totalBST = catalan_rightBST * catalan_leftBST ;
document.write( totalBST + " " );
}
</script>
|
Time Complexity: O(N2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...