Monday, January 12, 2015

BinaryTrees implementation and check if BST


public class BinaryTrees {
   
    private Node root;
    /**
     *Static Node class , an inner class
     */
    public static class Node {
        Node left;
        Node right;
        int data;
       
        Node(int newData){
            left=null;
            right=null;
            data=newData;
        }
       
       
    }
    /**
     * Constructor where , root is initilaized to null
     */
    public void BinaryTrees(){
        root=null;
    }
   
    public boolean lookup(int data){
        return(lookup(root,data));
    }

    private boolean lookup(Node node, int data) {
        if(node==null){
            return false;
        }
        if (data==node.data){
            return true;
        }else if(data<node.data){
            return (lookup(node.left,data));
        }else{
            return (lookup(node.right,data));
        }
   
    }

   
    public  void insert(int data){
        root=insert(root,data);
   
    }

    private Node insert(Node node, int data) {
        if(node ==null){
            node=new Node(data);
        }
        else{
            if(node.data < data){
                node.left=insert(node.left,data);
            }else{
                node.right=insert(node.right,data);
            }
        }
        return node;
    }
   
    public int size(){
        return size(root);
    }

    private int size(Node node) {
        if(node==null){
            return 0;
        }else{
            return(size(node.left)+1+size(node.right));
        }
       
    }
   
    public void printInorder(){
         printInorder(root);
    }

    private void printInorder(Node node) {
        if(node == null){
            return;
        }
        printInorder(node.left);
        System.out.print(node.data + "  ");
        printInorder(node.right);
    }
   
   
    public void printPostorder(){
        printPostorder(root);
    }

    private void printPostorder(Node node) {
        if(node == null){
            return;
        }
        printPostorder(node.left);
        printPostorder(node.right);
        System.out.print(node.data + "  ");
    }
   
    public void printPreorder(){
        printPreorder(root);
    }

    private void printPreorder(Node node) {
        if(node == null){
            return;
        }
        System.out.print(node.data + "  ");
        printPreorder(node.left);
        printPreorder(node.right);
       
    }
   
   
    /**
     Given a binary tree, prints out all of its root-to-leaf
     paths,per line.
     **/
    public void printPaths(){
        int []path=new int[1000];
        printPaths(root,path,0);
       
    }
/**
 * Recursive operation
 * @param node
 * @param path
 * @param i
 */
       
    private void printPaths(Node node, int[] path, int i) {
        if (node == null){
            return ;
        }
       
        path[i] = node.data;
        i++;
       
        if(node.left==null && node.right==null){
            printArray(path,i);
        }else{
            printPaths(node.left,path,i);
            printPaths(node.right,path,i);
        }
       

    }

    private void printArray(int[] path, int i) {
        int j;
        for (j=0;j<=i;j++){
            System.out.println(path[j] + " ");
        }
        System.out.println();
    }
   
    /**
     *
     * Uses a recursive helper that recurs over the tree, swapping the left/right pointers.
     *
     *              4                    4
     *             / \                     / \
     *            2   5 ------->       5   2
     *           / \                      / \
     *       1   3                    3   1
     *      
     * Changes the tree into its mirror image  
     */
   
    public void mirrorTheTree(){
        mirrorTheTree(root);
    }

    private void mirrorTheTree(Node node) {
        if(node !=null){
            mirrorTheTree(node.left);
            mirrorTheTree(node.right);
            Node temp=node.left;
            node.left=node.right;
            node.right=temp;
        }
       
    }
   
    /**
     Changes the tree by inserting a duplicate node
     on each nodes's .left.
   
     So the tree...
        2
       / \
      1   3

     Is changed to...
           2
          / \
         2   3
        /   /
       1   3
      /
     1

     Uses a recursive helper to recur over the tree
     and insert the duplicates.
    */
   
    public void createDoubleTree(){
        createDoubleTree(root);
    }

    private void createDoubleTree(Node node) {
        Node oldLeft;
        if (node==null){
            return;
        }
        createDoubleTree(node.left);
        createDoubleTree(node.right);
        oldLeft=node.left;
        node.left=new Node(node.data);
        node.left.left=oldLeft;
       
    }
   
    public boolean checkIfTreeSame(BinaryTrees other){
        return checkIfTreeSame(root, other.root);
    }

    private boolean checkIfTreeSame(Node node, Node node1) {
        if(node == null && node1==null){
            return true;
        }else if(node != null && node1 !=null){
            return (node.data==node1.data && checkIfTreeSame(node.left,node1.left) && checkIfTreeSame(node.right,node1.right));
        }else{
       
        return false;
        }
    }
   
    public static int countTrees(int keys){
        if(keys<=1){
            return 1;
        }
        else{
            int sum=0;
            int left,right,root;
            for(root=1;root<keys;root++){
                left=countTrees(root-1);
                right=countTrees(keys-root);
                sum+=left*right;
               
            }
            return sum;
        }
    }
   
    /**
      Efficient BST helper -- Given a node, and min and max values,
      recurs down the tree to verify that it is a BST, and that all
      its nodes are within the min..max range. Works in O(n) time --
      visits each node only once.
    */
    public boolean isBST(){
        return isBST(root,Integer.MIN_VALUE,Integer.MAX_VALUE);
    }

    private boolean isBST(Node node, int minValue, int maxValue) {
        if(node==null){
            return true;
        }else{
            boolean leftOk=isBST(node.left,  minValue,node.data);
            if(!leftOk){
                return false;
            }
            boolean rightOk=isBST(node.right,node.data+1,maxValue);
                return rightOk;
        }
       
       
    }
   
}