Build a Binary Search Tree from a Preorder Sequence

Given a distinct sequence of keys which represents preorder traversal of a binary search tree (BST), construct the tree from the postorder sequence.


 

For example, below BST should be constructed for preorder traversal { 15, 10, 8, 12, 20, 16, 25 }

BST

 
For a given preorder sequence, we can easily build a BST by recursively repeating the following steps for all keys in it.

  1. Construct the root node of BST which would be the first key in the preorder sequence.
     
  2. Find index i of the first key in the preorder sequence which is greater than the root node.
     
  3. Recur for the left sub-tree with keys in the preorder sequence that appears before the i’th index (excluding first index).
     
  4. Recur for the right sub-tree with keys in the preorder sequence that appears after the i’th index (including i’th index).

 
To make context more clear, let’s consider the preorder traversal {15, 10, 8, 12, 20, 16, 25}

  1. The first item in the preorder sequence 15 becomes the root node.
     
  2. Since 20 is the first key in the preorder sequence which greater than the root node, left sub-tree consists of keys {10, 8, 12} and right sub-tree consists of keys {20, 16, 25}.
     
  3. To construct the complete BST, recursively repeat the above steps for preorder sequence {10, 8, 12} and {20, 16, 25}.
     

This is demonstrated below in C –

 

Download   Run Code

Output:

Inorder Traversal of BST is : 8 10 12 15 16 20 25

 
The time complexity of above solution is O(n2) and takes O(n) extra space for the call stack. We can reduce the time complexity to O(n) by following a different approach that doesn’t involve searching for index which separates the keys of left and right sub-tree in preorder sequence.

We know that in a BST, each node has key which is greater than all keys present in its left sub-tree, and less than the keys present in the right sub-tree. The idea to pass the information regarding the valid range of keys for current root node and its children in the recursion itself.

We start by setting the range as [INT_MIN, INT_MAX] for the root node. This means that the root node and any of its children can have keys in the ranging between INT_MIN and INT_MAX. Like previous approach, we construct the root node of BST from the first item in the preorder sequence. Suppose the root node has value x, we recur for right sub-tree with range (x, INT_MAX) and recur for left sub-tree with range [INT_MIN, x). To construct the complete BST, we recursively set the range for each recursive call and simply return if next element of preorder traversal is out of the valid range.

Here’s a C++ program that demonstrates it:

 

Download   Run Code

Output:

Inorder Traversal of BST is : 8 10 12 15 16 20 25

 
Thanks for reading.

Please use our online compiler to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 


Leave a Reply

avatar
  Subscribe  
Notify of