Construct BST from given preorder traversal | Set 2 | GeeksforGeeks
Related: Construct a Binary Search Tree from given postorder
Given preorder traversal of a binary search tree, construct the BST.
public Node constructTree(int[] preorder, int data, int min, int max) {
if (pIndex < preorder.length) {
if (preorder[pIndex] > min && preorder[pIndex] < max) {
Node root = new Node(data);
pIndex++;
if (pIndex < preorder.length) { // if not needed
// nodes lies between min and data will create left subtree
root.left = constructTree(preorder, preorder[pIndex], min,
data);
// nodes lies between data and max will create right subtree
root.right = constructTree(preorder, preorder[pIndex],
data, max);
}
return root;
}
}
return null;
}
Node root = p.constructTree(preOrder, preOrder[0], Integer.MIN_VALUE,
Integer.MAX_VALUE);
Stack<Node> s = new Stack<Node>();
Node root = new Node(preorder[0]);
s.add(root);
for (int i = 1; i < preorder.length; i++) {
Node x = null;
while (!s.isEmpty() && preorder[i] > s.peek().data) {
x = s.pop();
}
if (x != null) {
x.right = new Node(preorder[i]);
s.push(x.right);
} else {
s.peek().left = new Node(preorder[i]);
s.push(s.peek().left);
}
}
return root;
}
Read full article from Construct BST from given preorder traversal | Set 2 | GeeksforGeeks
Related: Construct a Binary Search Tree from given postorder
Given preorder traversal of a binary search tree, construct the BST.
1. Create an empty stack.
2. Make the first value as root. Push it to the stack.
3. Keep on popping while the stack is not empty and the next value is greater than stack’s top value. Make this value as the right child of the last popped node. Push the new node to the stack.
4. If the next value is less than the stack’s top value, make this value as the left child of the stack’s top node. Push the new node to the stack.
Time Complexity: O(n). The complexity looks more from first look. If we take a closer look, we can observe that every item is pushed and popped only once. So at most 2n push/pop operations are performed in the main loops of constructTree(). Therefore, time complexity is O(n).
http://algorithms.tutorialhorizon.com/construct-binary-search-tree-from-a-given-preorder-traversal-using-recursion/
http://algorithms.tutorialhorizon.com/construct-binary-search-tree-from-a-given-preorder-traversal-using-recursion/
Solution to the problem is similar to isBST Max-Min Solution.
“Your root value can have any value between -∞ to + ∞, say it is 30 here, When you validate the right child of 30, it can take any value between 30 and + ∞. When you validate the left child of 30, it can take any value between — ∞ and 30. likewise
So the idea is Pass the minimum and maximum values between which the node’s value must lie.
- Example: int[] preOrder = { 20, 10, 5, 1, 7, 15, 30, 25, 35, 32, 40 };
- First element in the preorder[] will definitely be the root, which is 20 here.
- we start with the range minimum = Integer.MIN_VALUE and maximum = Interger.MAX_VALUE, so your root can take any value between this range.
- So when putting the left node of 20(root), it must lie within the range to minimum = Integer.MIN_VALUE and maximum = 20, so check the next element in the preorder[], if it lies in this range, make it the left child to the root, else it must the the right chlid of the root and so on. See the figure for better understanding. ( see the execution sequence)
- Solve it recursively.
public Node constructTree(int[] preorder, int data, int min, int max) {
if (pIndex < preorder.length) {
if (preorder[pIndex] > min && preorder[pIndex] < max) {
Node root = new Node(data);
pIndex++;
if (pIndex < preorder.length) { // if not needed
// nodes lies between min and data will create left subtree
root.left = constructTree(preorder, preorder[pIndex], min,
data);
// nodes lies between data and max will create right subtree
root.right = constructTree(preorder, preorder[pIndex],
data, max);
}
return root;
}
}
return null;
}
Node root = p.constructTree(preOrder, preOrder[0], Integer.MIN_VALUE,
Integer.MAX_VALUE);
- Example: int[] preOrder = { 20, 10, 5, 1, 7, 15, 30, 25, 35, 32, 40 };
- Use Stack.
- First element in the preorder[] will definitely be the root, which is 20 here.
- Create a node with data 20 and push it in Stack.
- Now take the next element (say ‘data’) from the preorder sequence.
- If ‘data’ is greater than the top item in the stack then keep popping out the nodes from the stack, keep storing it in temp node till the top node in stack is less than the ‘data’.
- create a node with ‘data’ and add it to the right of temp node and push the temp node to stack.
- If ‘data’ is less than the top item in the stack then create a node with ‘data’ and add it to the left of top node in stack and push it in the stack.
- Repeat the above two steps till all the elements in preorder[] is over.
- return the root
Stack<Node> s = new Stack<Node>();
Node root = new Node(preorder[0]);
s.add(root);
for (int i = 1; i < preorder.length; i++) {
Node x = null;
while (!s.isEmpty() && preorder[i] > s.peek().data) {
x = s.pop();
}
if (x != null) {
x.right = new Node(preorder[i]);
s.push(x.right);
} else {
s.peek().left = new Node(preorder[i]);
s.push(s.peek().left);
}
}
return root;
}
Node* constructTree (
int
pre[],
int
size )
{
// Create a stack of capacity equal to size
Stack* stack = createStack( size );
// The first element of pre[] is always root
Node* root = newNode( pre[0] );
// Push root
push( stack, root );
int
i;
Node* temp;
// Iterate through rest of the size-1 items of given preorder array
for
( i = 1; i < size; ++i )
{
temp = NULL;
/* Keep on popping while the next value is greater than
stack's top value. */
while
( !isEmpty( stack ) && pre[i] > peek( stack )->data )
temp = pop( stack );
// Make this greater value as the right child and push it to the stack
if
( temp != NULL)
{
temp->right = newNode( pre[i] );
push( stack, temp->right );
}
// If the next value is less than the stack's top value, make this value
// as the left child of the stack's top node. Push the new node to stack
else
{
peek( stack )->left = newNode( pre[i] );
push( stack, peek( stack )->left );
}
}
return
root;
}