Total number of possible Binary Search Trees with n different keys (countBST(n)) = Catalan number Cn = (2n)! / ((n + 1)! * n!)
For n = 0, 1, 2, 3, … values of Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, …. So are numbers of Binary Search Trees.
Total number of possible Binary Trees with n different keys (countBT(n)) = countBST(n) * n!
Below is code for finding count of BSTs and Binary Trees with n numbers. The code to find n’th Catalan number is taken from here.
Count of BST with 5 nodes is 42 Count of binary trees with 5 nodes is 5040
Proof of Enumeration
Consider all possible binary search trees with each element at the root. If there are n nodes, then for each choice of root node, there are n – 1 non-root nodes and these non-root nodes must be partitioned into those that are less than a chosen root and those that are greater than the chosen root.
Let’s say node i is chosen to be the root. Then there are i – 1 nodes smaller than i and n – i nodes bigger than i. For each of these two sets of nodes, there is a certain number of possible subtrees.
Let t(n) be the total number of BSTs with n nodes. The total number of BSTs with i at the root is t(i – 1) t(n – i). The two terms are multiplied together because the arrangements in the left and right subtrees are independent. That is, for each arrangement in the left tree and for each arrangement in the right tree, you get one BST with i at the root.
Summing over i gives the total number of binary search trees with n nodes.
The base case is t(0) = 1 and t(1) = 1, i.e. there is one empty BST and there is one BST with one node.
Also, the relationship countBT(n) = countBST(n) * n! holds. As for every possible BST, there can have n! binary trees where n is the number of nodes in BST.
This article is contributed by Shubham Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Total number of possible Binary Search Trees using Catalan Number
- Count the Number of Binary Search Trees present in a Binary Tree
- Binary Search Tree | Set 1 (Search and Insertion)
- Construct all possible BSTs for keys 1 to N
- Merge Two Balanced Binary Search Trees
- Print Common Nodes in Two Binary Search Trees
- Self-Balancing-Binary-Search-Trees (Comparisons)
- Check whether the two Binary Search Trees are Identical or Not
- Anagram Substring Search (Or Search for all permutations)
- Find all possible binary trees with given Inorder Traversal
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
- How to print maximum number of A's using given four keys
- Difference between Binary Tree and Binary Search Tree
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Binary Tree to Binary Search Tree Conversion
- Binary Tree to Binary Search Tree Conversion using STL set
- Total number of non-decreasing numbers with n digits
- Total number of decreasing paths in a matrix
- Total number of different staircase that can made from N boxes
- Minimize the total number of teddies to be distributed