forked from 790hanu/Annex-qr-code-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree
42 lines (42 loc) · 1.51 KB
/
BinarySearchTree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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));
}
}