Count the number of Binary Tree possible for a given Preorder Sequence length n.
Input : n = 1 Output : 1 Input : n = 2 Output : 2 Input : n = 3 Output : 5
In Preorder traversal, we process the root node first, then traverse the left child node and then right child node.
For example preorder traversal of below tree is 1 2 4 5 3 6 7
Finding number of trees with given Preorder:
Number of Binary Tree possible if such a traversal length (let’s say n) is given.
Let’s take an Example : Given Preorder Sequence –> 2 4 6 8 10 (length 5).
- Assume there is only 1 node (that is 2 in this case), So only 1 Binary tree is Possible
- Now, assume there are 2 nodes (namely 2 and 4), So only 2 Binary Tree are Possible:
- Now, when there are 3 nodes (namely 2, 4 and 6), So Possible Binary tree are 5
- Consider 4 nodes (that are 2, 4, 6 and 8), So Possible Binary Tree are 14.
Let’s say BT(1) denotes number of Binary tree for 1 node. (We assume BT(0)=1)
BT(4) = BT(0) * BT(3) + BT(1) * BT(2) + BT(2) * BT(1) + BT(3) * BT(0)
BT(4) = 1 * 5 + 1 * 2 + 2 * 1 + 5 * 1 = 14
- Similarly, considering all the 5 nodes (2, 4, 6, 8 and 10). Possible number of Binary Tree are:
BT(5) = BT(0) * BT(4) + BT(1) * BT(3) + BT(2) * BT(2) + BT(3) * BT(1) + BT(4) * BT(0)
BT(5) = 1 * 14 + 1 * 5 + 2 * 2 + 5 * 1 + 14 * 1 = 42
NOTE* Since we have already calculated for 1, 2 and 3 nodes. We don’t need to evaluate them again for successive nodes.
Hence, Total binary Tree for Pre-order sequence of length 5 is 42.
We use Dynamic programming to calculate the possible number of Binary Tree. We take one node at a time and calculate the possible Trees using previously calculated Trees.
Total Possible Binary Trees are : 42
This can also be done using 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.
This article is contributed by Shubham Rana. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Total number of possible Binary Search Trees and Binary Trees with n keys
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check if a given array can represent Preorder Traversal of Binary Search Tree
- Construct Full Binary Tree from given preorder and postorder traversals
- Calculate depth of a full Binary tree from Preorder
- Preorder Successor of a Node in Binary Tree
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Modify a binary tree to get preorder traversal using right pointers only
- Preorder predecessor of a Node in Binary Tree
- Find n-th node in Preorder traversal of a Binary Tree
- Construct a Maximum Binary Tree from two given Binary Trees
- Count the Number of Binary Search Trees present in a Binary Tree
- Construct Tree from given Inorder and Preorder traversals
- Construct a special tree from given preorder traversal
- Construct BST from given preorder traversal | Set 2
- Print Postorder traversal from given Inorder and Preorder traversals
- Check if given Preorder, Inorder and Postorder traversals are of same tree | Set 2
- Find Leftmost and Rightmost node of BST from its given preorder traversal
- Check if given Preorder, Inorder and Postorder traversals are of same tree