In this article, first count of possible BST (Binary Search Trees)s is discussed, then construction of all possible BSTs.
How many structurally unique BSTs for keys from 1..N?
For example, for N = 2, there are 2 unique BSTs 1 2 \ / 2 1 For N = 3, there are 5 possible BSTs 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
We strongly recommend you to minimize your browser and try this yourself first.
We know that all node in left subtree are smaller than root and in right subtree are larger than root so if we have ith number as root, all numbers from 1 to i-1 will be in left subtree and i+1 to N will be in right subtree. If 1 to i-1 can form x different trees and i+1 to N can from y different trees then we will have x*y total trees when ith number is root and we also have N choices for root also so we can simply iterate from 1 to N for root and another loop for left and right subtree. If we take a closer look, we can notice that the count is basically n’th Catalan number. We have discussed different approaches to find n’th Catalan number here.
How to construct all BST for keys 1..N?
The idea is to maintain a list of roots of all BSTs. Recursively construct all possible left and right subtrees. Create a tree for every pair of left and right subtree and add the tree to list. Below is detailed algorithm.
1) Initialize list of BSTs as empty. 2) For every number i where i varies from 1 to N, do following ......a) Create a new node with key as 'i', let this node be 'node' ......b) Recursively construct list of all left subtrees. ......c) Recursively construct list of all right subtrees. 3) Iterate for all left subtrees a) For current leftsubtree, iterate for all right subtrees Add current left and right subtrees to 'node' and add 'node' to list.
Below is implementation of above idea.
Preorder traversals of all constructed BSTs are 1 2 3 1 3 2 2 1 3 3 1 2 3 2 1
This article is contributed by Utkarsh Trivedi. 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.
- Check if two given key sequences construct same BSTs
- Check if two BSTs contain same set of elements
- Nodes from given two BSTs with sum equal to X
- Count pairs from two BSTs whose sum is equal to a given value x
- Print all pairs from two BSTs whose sum is greater than the given value
- Split a BST into two balanced BSTs based on a value K
- Find pairs with given sum such that pair elements lie in different BSTs
- Total number of BSTs using array elements
- Merge two BSTs with constant extra space
- Check for Identical BSTs without building the trees
- Merge two BSTs with limited extra space
- Generate two BSTs from the given array such that maximum height among them is minimum
- Remove BST Keys in a given Range
- BST to a Tree with sum of all smaller keys
- Remove BST keys outside the given range
- Print BST keys in the given range
- How to print maximum number of A's using given four keys
- Print BST keys in given Range | O(1) Space
- Construct BST from given preorder traversal | Set 1
- Construct BST from given preorder traversal | Set 2