📜  使用数组元素的BST总数

📅  最后修改于: 2021-04-29 01:48:10             🧑  作者: Mango

先决条件:带有n个键的可能的二进制搜索树总数
给定N个整数的数组arr [] 。任务是计算可以使用arr []中元素的每个节点作为根节点进行二进制搜索树的数量。

例子:

方法:
二进制搜索树(BST)的总数由加泰罗尼亚语数字给出:

Cn = (2n)!/(( n+1)!*n!) 
where n = number of distinct keys.
    对于arr []中的每个元素:
  1. 计算少于当前节点的元素数量(例如c1 )。
  2. 计算大于当前节点的元素数量(例如c2 )。
  3. 然后,可以使用当前元素作为根节点来形成二叉搜索树(BST)的总数等于使用c1个元素形成的BST总数与使用c2个元素形成的BST总数的乘积。
    Total Number of BST = Cc1*Cc2
    

下面是上述方法的实现:

C++
// C++ implementation of the above code 
#include 
using namespace std;
  
// A function to find factorial of a 
// given number 
int fact(int num) 
{ 
    int fact = 1; 
      
    while(num > 1) 
    { 
        fact *= num; 
        num -= 1; 
    } 
    return fact; 
} 
  
// Find nth catalan number 
int catalan(int n) 
{ 
    return fact(2 * n)/(fact(n) * fact(n + 1)) ; 
} 
  
// Driver Code 
int main() 
{ 
      
    // size of arr[] 
    int n = 5; 
      
    // Elements in arr[] 
    int arr[] = {1, 2, 3, 4, 5}; 
    int i,k; 
    for(k = 0; k < n; k++) 
    { 
        int s = 0; 
      
        // Count the number of element 
        // less than current element 
        // in arr[k] 
        for(i = 0; i < n; i++) 
        { 
            if (arr[i] < arr[k]) 
                s += 1 ; 
        } 
  
        // Here s = number of node in left 
        // BST and (n-s-1) = number of node 
        // in right BST 
        // Find number of BST using elements 
        // in left BST 
        int catalan_leftBST = catalan(s) ; 
          
        // Find number of BST using elements 
        // in right BST 
        int catalan_rightBST = catalan(n - s - 1) ; 
          
        // Find total number of BST 
        int totalBST = catalan_rightBST * catalan_leftBST ; 
          
        // Print total BST count 
        cout<< totalBST <<  " " ; 
  
    } 
} 
  
// This code is contributed by AnkitRai01


Java
// java implementation of the above code
public class GFG 
{
      
// A function to find factorial of a 
// given number 
static int fact(int num)
{
    int fact = 1; 
      
    while(num > 1)
    {
        fact *= num; 
        num -= 1; 
    }
    return fact; 
    }
  
// Find nth catalan number 
static int catalan(int n)
{
    return fact(2 * n)/(fact(n) * fact(n + 1)) ;
}
  
// Driver Code 
public static void main (String[] args) 
{
      
    // size of arr[] 
    int n = 5;
      
    // Elements in arr[] 
    int arr[] = {1, 2, 3, 4, 5};
    int i,k;
    for(k = 0; k < n; k++)
    { 
        int s = 0;
      
        // Count the number of element 
        // less than current element 
        // in arr[k] 
        for(i = 0; i < n; i++)
        {
            if (arr[i] < arr[k])
                s += 1 ;
        }
  
        // Here s = number of node in left 
        // BST and (n-s-1) = number of node 
        // in right BST 
        // Find number of BST using elements 
        // in left BST 
        int catalan_leftBST = catalan(s) ;
          
        // Find number of BST using elements 
        // in right BST 
        int catalan_rightBST = catalan(n - s - 1) ;
          
        // Find total number of BST 
        int totalBST = catalan_rightBST * catalan_leftBST ;
          
        // Print total BST count 
        System.out.print(totalBST + " ") ; 
  
    }
}
}
  
// This code is contributed by AnkitRai01


Python3
# A function to find factorial of a 
# given number 
def fact(num): 
    fact = 1; 
      
    while(num>1): 
        fact = fact * num; 
        num = num-1; 
    return fact; 
  
# Find nth catalan number
def catalan(n):
    return fact(2 * n)//(fact(n)*fact(n + 1))
  
# Driver Code 
if __name__ == '__main__':
      
    # size of arr[]
    n = 5
      
    # Elements in arr[]
    arr = [1, 2, 3, 4, 5]
  
    for k in range(n):
        s = 0
      
        # Count the number of element
        # less than current element 
        # in arr[k]
        for i in range(n):
            if arr[i] < arr[k]:
                s+= 1
  
        # Here s = number of node in left
        # BST and (n-s-1) = number of node 
        # in right BST
        # Find number of BST using elements 
        # in left BST
        catalan_leftBST = catalan(s)
          
        # Find number of BST using elements
        # in right BST
        catalan_rightBST = catalan(n-s-1)
          
        # Find total number of BST 
        totalBST = catalan_rightBST * catalan_leftBST
          
        # Print total BST count
        print(totalBST, end =" ")


C#
// C# implementation of the above code
using System;
  
class GFG 
{
       
// A function to find factorial of a 
// given number 
static int fact(int num)
{
    int fact = 1; 
       
    while(num > 1)
    {
        fact *= num; 
        num -= 1; 
    }
    return fact; 
    }
   
// Find nth catalan number 
static int catalan(int n)
{
    return fact(2 * n)/(fact(n) * fact(n + 1)) ;
}
   
// Driver Code 
public static void Main(String[] args) 
{
       
    // size of []arr 
    int n = 5;
       
    // Elements in []arr 
    int []arr = {1, 2, 3, 4, 5};
    int i,k;
    for(k = 0; k < n; k++)
    { 
        int s = 0;
       
        // Count the number of element 
        // less than current element 
        // in arr[k] 
        for(i = 0; i < n; i++)
        {
            if (arr[i] < arr[k])
                s += 1 ;
        }
   
        // Here s = number of node in left 
        // BST and (n-s-1) = number of node 
        // in right BST 
        // Find number of BST using elements 
        // in left BST 
        int catalan_leftBST = catalan(s) ;
           
        // Find number of BST using elements 
        // in right BST 
        int catalan_rightBST = catalan(n - s - 1) ;
           
        // Find total number of BST 
        int totalBST = catalan_rightBST * catalan_leftBST ;
           
        // Print total BST count 
        Console.Write(totalBST + " ") ; 
   
    }
}
}
  
// This code is contributed by 29AjayKumar


输出:
14 5 4 5 14

时间复杂度: O(N 2 )