Construct binary search tree from given preorder traversal
Reference
import java.util.ArrayDeque;
class ConstructBSTGivenPreorder {
private static Node root;
static void constructFromPreorderIterative(int[] preorder) {
// create root
root = new Node(preorder[0]);
// create statck to track left child of tree during construction of tree
ArrayDeque stack = new ArrayDeque<>();
// taking variable to track current node
Node current = root;
// insert all elements
for(int i = 1; i < preorder.length; i++) {
// create a new node
Node node = new Node(preorder[i]);
if(node.data < current.data) {
current.left = node;
stack.addFirst(current);
current = node;
} else if(stack.isEmpty() || node.data < stack.peekFirst().data) {
current.right = node;
current = node;
} else {
while(!stack.isEmpty() && node.data > stack.peekFirst().data) {
current = stack.peekFirst(); // stack.peek
stack.pollFirst(); // stack.pop
}
current.right = node;
current = node;
}
}
}
static void constructFromPreorderIterative2(int[] preorder) {
// create root
root = new Node(preorder[0]);
// create statck to track left child of tree during construction of tree
ArrayDeque stack = new ArrayDeque<>();
// taking variable to track current node
Node current = root;
int i = 1;
// insert all elements
while(i < preorder.length) {
if(preorder[i] < current.data) {
// create a new node
Node node = new Node(preorder[i++]);
current.left = node;
stack.addFirst(current);
current = node;
} else if(stack.isEmpty() || preorder[i] < stack.peekFirst().data) {
// create a new node
Node node = new Node(preorder[i++]);
current.right = node;
current = node;
} else {
current = stack.peekFirst(); // stack.peek
stack.pollFirst(); // stack.pop
}
}
}
static Node constructFromPreorderRecursive(Node node, int data) {
if(node == null) {
return new Node(data);
} else if(data < node.data) {
node.left = constructFromPreorderRecursive(node.left, data);
} else if(data > node.data) {
node.right = constructFromPreorderRecursive(node.right, data);
}
return node;
}
static void preOrderTraversal(Node root) {
if(root == null) {
return;
}
System.out.print(root.data + " ");
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
public static void main (String[] args) {
int[] preorder = new int[] {30, 20, 10, 15, 25, 40, 50, 45};
// recursive
root = new Node(preorder[0]);
for(int i = 1; i < preorder.length; i++) {
constructFromPreorderRecursive(root, preorder[i]);
}
System.out.println("Preorder traversal : ");
preOrderTraversal(root);
// iterative
constructFromPreorderIterative(preorder);
System.out.println("\nPreorder traversal : ");
preOrderTraversal(root);
// iterative 2
constructFromPreorderIterative2(preorder);
System.out.println("\nPreorder traversal : ");
preOrderTraversal(root);
}
private static class Node {
Node left;
int data;
Node right;
public Node(int data) {
this.data = data;
}
}
}
Output
Preorder traversal :
30 20 10 15 25 40 50 45
Preorder traversal :
30 20 10 15 25 40 50 45
Preorder traversal :
30 20 10 15 25 40 50 45
Comments
Post a Comment