📅  最后修改于: 2020-10-15 06:40:30             🧑  作者: Mango
在此程序中,我们需要找出可以用n个值构造的二叉搜索树的总数。下图显示了一个可能的二进制搜索树,其键值为3。因此,我们总共可以构建五棵二进制搜索树。当我们选择节点1作为根节点时,我们得到两棵树。类似地,当我们选择3作为根节点时,一棵树以2为根节点,两棵树。
此方法涉及递归选择一个节点作为根节点,并创建可能的二进制搜索树。
一种简单的计算可能的二叉搜索树总数的方法是通过加泰罗尼亚语数字:
Cn = (2n)! / n! *(n+1)!
#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
#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
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
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
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