📜  使用数组元素的 BST 总数

📅  最后修改于: 2021-09-06 11:27:15             🧑  作者: Mango

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

方法:
可能的二叉搜索树(BST)的总数由加泰罗尼亚数给出:

Cn = (2n)!/(( n+1)!*n!) 
where n = number of distinct keys.

  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


Javascript


输出:
14 5 4 5 14

时间复杂度: O(N 2 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live