Construct tree | Given inorder and preorder | Recursive

Iterative version.

import java.util.concurrent.atomic.AtomicInteger;
import java.util.Map;
import java.util.HashMap;

class ConstructTree {
    
    static Node constructRecursive(int[] preorder, Map inorderElementMap, 
        AtomicInteger inorderCounter, int inStartIndex, int inEndIndex) {
        // Corner case for leaf nodes 
        if(inStartIndex > inEndIndex) {
            return null;
        }
        
        Node root = new Node(preorder[inorderCounter.getAndIncrement()]);
        
        // get the root node index in sequence `inorder[]` to determine the
        // left and right subtree boundary
        int index = inorderElementMap.get(root.data);
        
        // construct left sub-tree
        root.left = constructRecursive(preorder, inorderElementMap, inorderCounter, 
        inStartIndex, index-1);
        // construct right sub-tree
        root.right = constructRecursive(preorder, inorderElementMap, inorderCounter, 
        index+1, inEndIndex);
        
        return root;
    }
    
    static Node construct(int[] preorder, int[] inorder) {
         // construct Map from inorder elements Map
        // it will help to find element in constant time as linear 
        // search take O(n) time 
        Map inorderElementMap = new HashMap<>();
        for(int i = 0; i < inorder.length; i++) {
            inorderElementMap.put(inorder[i], i);
        }
        
        // global integer counter 
        AtomicInteger inorderCounter = new AtomicInteger(0);
        
        // call recursive construct method and return tree 
        return constructRecursive(preorder, inorderElementMap, inorderCounter, 0, preorder.length-1);
    }
    
    static void inorderTraversal(Node root) {
        if(root == null) {
            return;
        }
        inorderTraversal(root.left);
        System.out.print(root.data + " ");
        inorderTraversal(root.right);
    }
    
    static void preorderTraversal(Node root) {
        if(root == null) {
            return;
        }
        System.out.print(root.data + " ");
        preorderTraversal(root.left);
        inorderTraversal(root.right);
    }
    
	public static void main (String[] args) {
	    int[] inorder = new int[] {4, 2, 5, 8, 1, 6, 3, 9, 7};
        //int[] preorder = new int[] {1, 2, 4, 5, 8, 3, 6, 7, 9};
        int[] preorder = new int[] {1, 2, 4, 5, 8, 6, 3, 9, 7};
        
        Node root = construct(preorder, inorder);
        
        // traverse the constructed tree
        System.out.print("The inorder traversal is ");
        inorderTraversal(root);
 
        System.out.print("\nThe preorder traversal is ");
        preorderTraversal(root);
	}
	
	private static class Node {
	    Node left;
	    int data;
	    Node right;
	    public Node(int data) {
	        this.data = data;
	    }
	}
}

Output
The inorder traversal is 4 2 5 8 1 6 3 9 7 
The preorder traversal is 1 2 4 5 8 6 3 9 7 

Comments

Popular posts from this blog

SQL basic interview question

gsutil Vs Storage Transfer Service Vs Transfer Appliance