📜  数据结构示例-用n键查找可能的二分搜索树的总数

📅  最后修改于: 2020-10-15 06:40:30             🧑  作者: Mango

问:程序以n键查找可能的二进制搜索树的总数。

说明

在此程序中,我们需要找出可以用n个值构造的二叉搜索树的总数。下图显示了一个可能的二进制搜索树,其键值为3。因此,我们总共可以构建五棵二进制搜索树。当我们选择节点1作为根节点时,我们得到两棵树。类似地,当我们选择3作为根节点时,一棵树以2为根节点,两棵树。

此方法涉及递归选择一个节点作为根节点,并创建可能的二进制搜索树。

一种简单的计算可能的二叉搜索树总数的方法是通过加泰罗尼亚语数字:

Cn = (2n)! / n! *(n+1)!

算法

  • 定义具有三个属性的Node类,即:左和右数据。在此,左代表节点的左子节点,右代表节点的右子节点。
  • 创建节点时,数据将传递到该节点的data属性,并且将左右两个都设置为null。
  • 定义另一个具有属性根的类。
    1. 表示树的根节点,并将其初始化为null。
  • numOfBST()将找出给定键的总可能的二进制搜索树:
    1. 它将通过调用factorial()来计算给定密钥的加泰罗尼亚语数字。
    2. 加泰罗尼亚数可以使用以下公式计算:Cn =(2n)! / n! *(n + 1)!
    3. Factorial()将计算给定数字的阶乘。

示例:

Python

#Represent a node of binary tree
class Node:
    def __init__(self,data):
        #Assign data to the new node, set left and right children to None
        self.data = data;
        self.left = None;
        self.right = None;
 
class BinarySearchTree:
    def __init__(self):
        #Represent the root of binary tree
        self.root = None;
        
    #factorial() will calculate the factorial of given number
    def factorial(self, num):
        fact = 1;
        if(num == 0):
            return 1;
        else:
            while(num > 1):
                fact = fact * num;
                num = num - 1;
            return fact;
    
    #numOfBST() will calculate the total number of possible BST by calculating Catalan Number for given key
    def numOfBST(self, key):
        catalanNumber = self.factorial(2 * key)/(self.factorial(key + 1) * self.factorial(key));
        return int(catalanNumber);
 
bt = BinarySearchTree();
 
#Display the total number of possible binary search tree with key 5
print("Total number of possible Binary Search Trees with given key: " + str(bt.numOfBST(5)));

输出:

Total number of possible Binary Search Trees with given key: 42

C

#include 
#include 
 
//Represent a node of binary tree
struct node{
    int data;
    struct node *left;
    struct node *right;
};
 
//Represent the root of binary tree
struct node *root = NULL;
 
//createNode() will create a new node
struct node* createNode(int data){
    //Create a new node
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    //Assign data to newNode, set left and right child to NULL
    newNode->data = data;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}
 
//factorial() will calculate the factorial of given number
int factorial(int num) {
    int fact = 1;
    if(num == 0)
        return 1;
    else {
        while(num > 1) {
            fact = fact * num;
            num--;
        }
        return fact;
    }
}
 
//numOfBST() will calculate the total number of possible BST by calculating Catalan Number for given key
int numOfBST(int key) {
    int catalanNumber = factorial(2 * key)/(factorial(key + 1) * factorial(key));
    return catalanNumber;
}
    
int main(){
    
    //Display total number of possible binary search tree with key 5
    printf("Total number of possible Binary Search Trees with given key: %d", numOfBST(5));
    
    return 0;
}

输出:

Total number of possible Binary Search Trees with given key: 42

JAVA

public class BinarySearchTree {
    
    //Represent the node of binary tree
    public static class Node{
        int data;
        Node left;
        Node right;
        
        public Node(int data){
            //Assign data to the new node, set left and right children to null
            this.data = data;
            this.left = null;
            this.right = null;
            }
        }
    
    //Represent the root of binary tree
    public Node root;
    
    public BinarySearchTree(){
        root = null;
    }
    
    //factorial() will calculate the factorial of given number
    public int factorial(int num) {
        int fact = 1;
        if(num == 0)
            return 1;
        else {
            while(num > 1) {
                fact = fact * num;
                num--;
            }
            return fact;
        }
    }
    
    //numOfBST() will calculate the total number of possible BST by calculating Catalan Number for given key
    public int numOfBST(int key) {
        int catalanNumber = factorial(2 * key)/(factorial(key + 1) * factorial(key));
        return catalanNumber;
    }
    
    public static void main(String[] args) {
        
        BinarySearchTree bt = new BinarySearchTree();
        
        //Display total number of possible binary search tree with key 5
        System.out.println("Total number of possible Binary Search Trees with given key: " + bt.numOfBST(5));
      }
}

输出:

Total number of possible Binary Search Trees with given key: 42

C#

using System;
namespace Tree 
{                     
    public class Program
    {
        //Represent a node of binary tree
        public class Node{
            public T data;
            public Node left;
            public Node right;
            
            public Node(T data) {
                //Assign data to the new node, set left and right children to null
                this.data = data;
                this.left = null;
                this.right = null;
            }
        }
        
        public class BinarySearchTree{
            //Represent the root of binary tree
            public Node root;
 
            public BinarySearchTree(){
                root = null;
            }
        
            //factorial() will calculate the factorial of given number
            public int factorial(int num) {
                int fact = 1;
                if(num == 0)
                    return 1;
                else {
                    while(num > 1) {
                        fact = fact * num;
                        num--;
                    }
                    return fact;
                }
            }
 
            //numOfBST() will calculate the total number of possible BST by calculating Catalan Number for given key
            public int numOfBST(int key) {
                int catalanNumber = factorial(2 * key)/(factorial(key + 1) * factorial(key));
                return catalanNumber;
            }
        }
        
        public static void Main()
        {
            BinarySearchTree bt = new BinarySearchTree();
        
            //Display total number of possible binary search tree with key 5
            Console.WriteLine("Total number of possible Binary Search Trees with given key: " + bt.numOfBST(5));                
        }    
    }

输出:

Total number of possible Binary Search Trees with given key: 42

PHP:




data = $data;
        $this->left = NULL;
        $this->right = NULL;
    }
}
class BinarySearchTree{
    //Represent the root of binary tree
    public $root;
    function __construct(){
        $this->root = NULL;
    }
    
    //factorial() will calculate the factorial of given number
    function factorial($num) {
        $fact = 1;
        if($num == 0)
            return 1;
        else {
            while($num > 1) {
                $fact = $fact * $num;
                $num--;
            }
            return $fact;
        }
    }
    
    //numOfBST() will calculate the total number of possible BST by calculating Catalan Number for given key
    function numOfBST($key) {
        $catalanNumber = $this->factorial(2 * $key)/($this->factorial($key + 1) * $this->factorial($key));
        return $catalanNumber;
    }
}
 
$bt = new BinarySearchTree();
        
//Display total number of possible binary search tree with key 5
print "Total number of possible Binary Search Trees with given key: " . $bt->numOfBST(5);      
?>


输出:

Total number of possible Binary Search Trees with given key: 42