Sunday, July 24, 2016

Data Structures - binary trees using JAVA

/**
 *
 * @author Mahi
 */
public class BinaryTreeApp {

   
    public static void main(String[] args) {
       
        //-----populating the binary tree ----//
       
        //Creating the Nodes
       
       
        //Create root node
        Node root = new Node(1);
       
        //Create main parents
        Node leftOne = new Node(3);
        Node rightOne = new Node(2);
       
        //Create right parent left child
        Node childLeft = new Node(4);
       
        //Create left parent right child
        Node childRight = new Node(5);
       
        //Connection the Nodes
       
        root.left = leftOne;
        root.right = rightOne;
       
        root.right.left = childLeft;
        root.right.right = childRight;
       
        //----- Testing NLR method -------//
        System.out.println("Nodes is Pre order NLR");
        root.preOrder(root);
       
       
        //------- Testing the LNR method ---------//
        System.out.println("Nodes is in order LNR");
        root.inOrer(root);
       
       
       
        //-------Morror function testing ---------//
        System.out.println("Setting the morror function..");
        root.mirror(root);
        System.out.println("After the mirror...");
        root.inOrer(root);
       
       
        //----Swapp Children------//
        System.out.println("Swapp Children begins...");
        root.swapChildren(root);
        System.out.println("After Swapp Children...");
        root.inOrer(root);
       
    }
   
}

class Node{
   
    int data;
    Node left;
    Node right;
   
    //Node for root.
    Node root;
   
    public Node(int i){
        data = i;
        left = right = null;
        root = null;
    }
   
    public void display(){
        System.out.println(data);
    }
   
   
    //Method for checking children for given parent
    public void printChilderens(Node current){
        System.out.println("Current node value " + current.data);
        System.out.println("Current node Left Child " + current.left.data);
        System.out.println("Current node Right Child " + current.right.data);
    }
   
   
    //NLR method
    public void preOrder(Node n){
        Node current = n;
        if(current!= null){
            System.out.println("Items is " + current.data);
            preOrder(current.left);
            preOrder(current.right);
           
        }
    }
  
    //LNR
    public void inOrer(Node n){
        Node current = n;
        if(current != null){
            inOrer(current.left);
            System.out.println("Items is : "+ current.data);
            inOrer(current.right);
        } 
    }

    //LRN
    public void postOrder(Node n){
        Node current = n;
        if(current != null){
            postOrder(current.left);
            postOrder(current.right);
            System.out.println("Item is "+ current.data);
        }
    }
   
    //This method mirror the B.tree
    public void mirror(Node n){
        Node current = n;
        if(current == null){
            return;
        }
        else{
           
            mirror(current.left);
            mirror(current.right);
           
            //swap nodes
            Node temp = current.left;
            current.left = current.right;
            current.right = temp;
           
       
        }
    }
   
    public void swapChildren(Node n){
        Node current = n;
         if(current == null){
            return;
        }
        else{
            //swap nodes
            Node temp = current.left;
            current.left = current.right;
            current.right = temp;
        }
    }
}