Thursday, 5 January 2017

Binary Tree size recursion and non recursion

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.

Write a program to find the size of a binary tree using recursion and non recursion

 */
package bin_tree_size_height_depth;

import java.util.Scanner;

/**
 *
 * @author shabhatn
 */

class binary_node
{
    binary_node left , right;
    int data;
   
    public binary_node()
    {
        left = right = null;
       
    }
   
    public binary_node(int data)
    {
        left = right = null;
        this.data = data;
    }

    public binary_node getLeft() {
        return left;
    }

    public void setLeft(binary_node left) {
        this.left = left;
    }

    public binary_node getRight() {
        return right;
    }

    public void setRight(binary_node right) {
        this.right = right;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
   
   
}

class binary_tree
{
    binary_node root;
    int size;
   
    public binary_tree()
    {
        root = null;
        size = 0;
    }
   
    public boolean isEmpty()
    {
        return root == null;
    }
   
    public void insert(int data)
    {
        root = insert_recursion(root,data);
    }
   
    public binary_node insert_recursion(binary_node node,int data)
    {
        if(node==null)
        {
            node = new binary_node(data);
        }
        else
        {
            if(node.getLeft()==null)
            {
                node.left = insert_recursion(node.left,data);
            }
            else
            {
                node.right = insert_recursion(node.right,data);
            }
        }
        return node;
    }
   
    public void insert_nonrecursion(int data)
    {
        binary_node nptr = new binary_node(data);
        binary_node temp;
        ll_queue lq = new ll_queue();
        if(root==null)
        {
            root = nptr;
        }
        else
        {
           
            lq.enqueue(root);
           
            while(!lq.isEmpty())
            {
                temp = lq.dequeue();
                if(temp.getLeft()!=null)
                {
                   
                    lq.enqueue(temp.getLeft());                  
                }
                else
                {
                    temp.setLeft(nptr);
                    break;
                }
                if(temp.getRight()!=null)
                {
                    lq.enqueue(temp.getRight());                  
                }
                else
                {
                    temp.setRight(nptr);
                    break;
                }
                 
            }
        }
       
    }
    public void Level_order_traversal()
    {
        LevelOrderTraversal(root);
    }
   
    public void LevelOrderTraversal(binary_node node)
    {
        ll_queue lq = new ll_queue();
        binary_node temp;
        if(node!=null)
        {
            lq.enqueue(node);
            while(!lq.isEmpty())
            {
                temp = lq.dequeue();
                System.out.print(temp.getData()+ " ");
                if(temp.getLeft()!=null)
                    lq.enqueue(temp.getLeft());
                if(temp.getRight()!=null)
                    lq.enqueue(temp.getRight());
               
            }
           
        }
    }
   
    public void size()
    {
        int x = size_recursion(root);
        System.out.print("size recursion-->" + x);
    }
   
    public int size_recursion(binary_node node)
    {
        int left , right , count=0;
        if(node==null)
            return 0;
        else
        {
            count++;
            left = size_recursion(node.left);
            right = size_recursion(node.right);
           
           count = left+right+count;
           
        }
        return count;
    }
   
    public int size_nonrecursion()
    {
        int count = 0;
       
        if(root==null)
            return 0;
        else
        {
            ll_queue lq = new ll_queue();
            binary_node temp;
            lq.enqueue(root);
            while(!lq.isEmpty())
            {
                temp = lq.dequeue();
                count++;
                if(temp.getLeft()!=null)
                    lq.enqueue(temp.getLeft());
                if(temp.getRight()!=null)
                    lq.enqueue(temp.getRight());
            }
            return count;
           
        }
    }
    public void display()
     {
         char c;
         Scanner sc = new Scanner(System.in);
         int data;
         do
         {
             data = sc.nextInt();
             insert(data);
            // insert_nonrecursion(data);
             System.out.println("do you want to insert more");
             c = sc.next().charAt(0);
         }while(c=='Y'||c=='y');
         System.out.println("Levelorder");
         Level_order_traversal();
         size();
         int x = size_nonrecursion();
         System.out.println("non recursion size-->"+x);
     }
   
}


class ll_queue
{
    ll_node front,rear;
    int size;
   
    public ll_queue()
    {
        front = rear = null;
        size = 0;
    }
    public boolean isEmpty()
    {
        return front==null;
    }
   
    public void enqueue(binary_node node)
    {
        ll_node nptr = new ll_node(node);
       
        if(front==null)
        {
            front = rear = nptr;
        }
        else
        {
            rear.setLink(nptr);
            rear = nptr;
        }
        size++;
    }
   
    public binary_node dequeue()
    {
        binary_node temp;
        if(front==null)
            return null;
        else
        {
            temp = front.getNode();
            front = front.getLink();
        }
                   
        return temp;
    }
   
}



class ll_node
{
    ll_node link;
    binary_node node;
   
    public ll_node()
    {
        link = null;
        node = null;
    }
   
    public ll_node(binary_node node)
    {
        link = null;
        this.node = node;
    }

    public ll_node getLink() {
        return link;
    }

    public void setLink(ll_node link) {
        this.link = link;
    }

    public binary_node getNode() {
        return node;
    }

    public void setNode(binary_node node) {
        this.node = node;
    }
   
   
   
}


public class Bin_tree_size_height_depth {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       
        binary_tree bt = new binary_tree();
        bt.display();
    }
   
}

Wednesday, 4 January 2017

Binary tree insertion without recursion

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.

Write a program for binary tree insertion without recursion

 */
package non_recursion_bin_tree_insertion;

import java.util.Scanner;

/**
 *
 * @author shabhatn
 */
class binary_node
{
    binary_node left ,right;
    int data;
   
    public binary_node()
    {
        left = right = null;
    }
   
    public binary_node(int data)
    {
        left = right = null;
        this.data = data;
    }

    public binary_node getLeft() {
        return left;
    }

    public void setLeft(binary_node left) {
        this.left = left;
    }

    public binary_node getRight() {
        return right;
    }

    public void setRight(binary_node right) {
        this.right = right;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
   
   
}

class binary_tree
{
    binary_node root;
    int size;
   
    public binary_tree()
    {
        root = null;
    }
   
    public boolean isEmpty()
    {
        return root==null;
    }
   
    public void insert(int data)
    {
       binary_node temp;
     
       binary_node nptr = new binary_node(data);
       ll_queue lq = new ll_queue();
     
       if(root==null)
       {
           root = nptr;
       }
       else
       {
           lq.enqueue(root);
           while(!lq.isEmpty())
           {
               temp = lq.dequeue();
             
              // System.out.println(temp.getData());
             
               if(temp.getLeft()!=null)
               {
                 
                   lq.enqueue(temp.getLeft());  
               }
               else
               {
                   temp.setLeft(nptr);
                   break;
               }
               if(temp.getRight()!=null)
               {
                 
                   lq.enqueue(temp.getRight());
                 
               }
               else
               {
                   temp.setRight(nptr);
                   break;
               }
           }
           lq.delete_queue();
       }
    }
     public void preorder()
    {
        preorder_traversal(root);
    }
    public void preorder_traversal(binary_node preO)
    {
          if(preO!=null)
        {
              System.out.print(preO.getData() + " ");
              preorder_traversal(preO.getLeft());
              preorder_traversal(preO.getRight());
           
        }
       
    }
    public void Postorder()
    {
        Postordertraversal(root);
    }
   
    public void Postordertraversal(binary_node node)
    {
        if(node!=null)
        {
            Postordertraversal(node.getLeft());
            Postordertraversal(node.getRight());
            System.out.print(node.getData()+ " ");
           
        }
    }

   
    public void Inorder()
    {
        Inordertraversal(root);
    }
   
    public void Inordertraversal(binary_node node)
    {
        if(node!=null)
        {
            Inordertraversal(node.getLeft());
            System.out.print(node.getData()+ " ");
            Inordertraversal(node.getRight());
        }
    }
   
    public void Level_order_traversal()
    {
        LevelOrderTraversal(root);
    }
   
    public void LevelOrderTraversal(binary_node node)
    {
        ll_queue lq = new ll_queue();
        binary_node temp;
        if(node!=null)
        {
            lq.enqueue(node);
            while(!lq.isEmpty())
            {
                temp = lq.dequeue();
                System.out.print(temp.getData()+ " ");
                if(temp.getLeft()!=null)
                    lq.enqueue(temp.getLeft());
                if(temp.getRight()!=null)
                    lq.enqueue(temp.getRight());
               
            }
            lq.delete_queue();
           
        }
    }
   
    public void display()
     {
         char c;
         Scanner sc = new Scanner(System.in);
         int data;
         do
         {
             data = sc.nextInt();
             insert(data);
             System.out.println("do you want to insert more");
             c = sc.next().charAt(0);
         }while(c=='Y'||c=='y');
         System.out.println("Preorder");
         preorder();
         System.out.println("Inorder");
         Inorder();
         System.out.println("Postorder");
         Postorder();
         System.out.println("Levelorder");
         Level_order_traversal();
     }
   
}

class ll_node
{
    ll_node link;
    binary_node data;
   
    public ll_node()
    {
        link = null;
        data = null;
    }
   
    public ll_node(binary_node data)
    {
        this.data = data;
        link = null;
    }

    public ll_node getLink() {
        return link;
    }

    public void setLink(ll_node link) {
        this.link = link;
    }

    public binary_node getData() {
        return data;
    }

    public void setData(binary_node data) {
        this.data = data;
    }
   
   
}

class ll_queue
{
    ll_node front , rear;
    int size;
   
    public ll_queue()
    {
        front = rear = null;
        size = 0;
    }
   
    public boolean isEmpty()
    {
        return front==null;
    }
   
    public void enqueue(binary_node data)
    {
        ll_node nptr = new ll_node(data);
       
        if(front==null)
        {
            front = rear = nptr;
        }
        else
        {
            rear.setLink(nptr);
            rear = nptr;
        }
        size++;
    }
   
    public binary_node dequeue()
    {
        binary_node data;
        if(isEmpty())
        {
            return null;
        }
        else
        {
            data = front.getData();
            front = front.getLink();
        }
        size--;
        return data;
    }
   
    public void delete_queue()
    {
        while(!isEmpty())
        {
            front = front.getLink();
            size--;
        }
       
    }
   
   
}


public class Non_recursion_bin_tree_insertion {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        binary_tree bt = new binary_tree();
        bt.display();
        // TODO code application logic here
    }
   
}

Tuesday, 3 January 2017

Search an element in binary tree using recursion and non recursion

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.

Write a program to search an element in binary tree using recursion and non recursion

 */
package search_binary_tree;

import java.util.Scanner;

/**
 *
 * @author shabhatn
 */

class ll_node
{
    ll_node link;
    binary_node data;
   
    public ll_node()
    {
        link = null;
        data = null;
    }
   
    public ll_node(binary_node data)
    {
        this.data = data;
        link = null;
    }

    public ll_node getLink() {
        return link;
    }

    public void setLink(ll_node link) {
        this.link = link;
    }

    public binary_node getData() {
        return data;
    }

    public void setData(binary_node data) {
        this.data = data;
    }
   
   
}

class ll_queue
{
    ll_node front , rear;
    int size;
   
    public ll_queue()
    {
        front = rear = null;
        size = 0;
    }
   
    public boolean isEmpty()
    {
        return front == null;
    }
   
    public void enqueue(binary_node data)
    {
        ll_node nptr = new ll_node(data);
       
        if(front==null)
        {
            front = rear = nptr;
        }
        else
        {
            rear.setLink(nptr);
            rear = nptr;
        }
        size++;
    }
   
    public binary_node dequeue()
    {
        binary_node node;
        if(isEmpty())
        {
            return null;
        }
        else
        {
            node = front.getData();
            front = front.getLink();
         
        }
        size--;
        return node;
    }
   
}



class binary_node
{
    binary_node left ,right;
    int data;
   
    public binary_node()
    {
        left = right = null;
    }
   
    public binary_node(int data)
    {
        left = right = null;
        this.data = data;
    }

    public binary_node getLeft() {
        return left;
    }

    public void setLeft(binary_node left) {
        this.left = left;
    }

    public binary_node getRight() {
        return right;
    }

    public void setRight(binary_node right) {
        this.right = right;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
   
   
}

class binary_tree
{
    binary_node root;
    int size;
   
    public binary_tree()
    {
        root = null;
    }
   
    public boolean isEmpty()
    {
        return root==null;
    }
   
    public void insert(int data)
    {
        root = insert_binary(root,data);
    }
   
    public binary_node insert_binary(binary_node noder, int data)
    {
       
       
        if(noder==null)
        {
            noder = new binary_node(data);
        }
        else
        {
            if(noder.getLeft()==null)
            {
                noder.left = insert_binary(noder.left,data);
            }
            else
            {
                noder.right = insert_binary(noder.right,data);
            }
        }
        return noder;
    }
   
    public void search(int data)
    {
        int d,d1;
        d = search(root,data);
        if(d!=-999)
        {
        System.out.println("data found-->" + d);
        }
        else
        {
            System.out.println("data not found recursioncode :Shas " +d );
        }
        d1 = search_nonrecursion(root,data);
        if(d1!=-998)
        {
        System.out.println("data found-->" + d);
        }
        else
        {
            System.out.println("data not found nonrecusrioncode :Shas " +d1);
        }
       
    }
   
    public int search(binary_node root,int data)
    {
        int root_val,left,right,ser=-999;
        if(root!=null)
        {
            root_val = root.getData();
            if(root_val==data)
            {
                return data;
            }
            left = search(root.left,data);
            if(left==data)
                return data;
            right = search(root.right,data);
            if(right==data)
                return data;          
           
        }
           
        return ser;
    }
   
    public int search_nonrecursion(binary_node root,int data)
    {
        binary_node temp;
        int ser = -998;
        ll_queue lq = new ll_queue();
        if(root==null)
            return -998;
        lq.enqueue(root);
        while(!lq.isEmpty())
        {
            temp = lq.dequeue();
            if(temp.getData()==data)
                return data;
            if(temp.getLeft()!=null)
            {
                lq.enqueue(temp.getLeft());
            }
            if(temp.getRight()!=null)
            {
                lq.enqueue(temp.getRight());
            }
        }

           
       
        return ser;
       
    }
   
     public void display()
     {
         char c;
         Scanner sc = new Scanner(System.in);
         int data;
         do
         {
             data = sc.nextInt();
             insert(data);
             System.out.println("do you want to insert more");
             c = sc.next().charAt(0);
         }while(c=='Y'||c=='y');
       
         System.out.println("Enter data to search");
          int d = sc.nextInt();
         search(d);
     }
   
   
}


public class Search_binary_tree {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        binary_tree br = new binary_tree();
       
        br.display();
        // TODO code application logic here
    }
   
}

Monday, 2 January 2017

Binary tree max value using recursion and non - recursion

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.

Write a program to find binary tree maximum value using recursion and non recursion

 */
package findmax_binary_tree;

import java.util.Scanner;

/**
 *
 * @author shabhatn
 */


    /**
     * @param args the command line arguments
     */

class ll_node
{
    ll_node link;
    node data;
   
    public ll_node()
    {
        link = null;
        data = null;
       
    }
   
    public ll_node(node data,ll_node dnode)
    {
        link = dnode;
        this.data = data;
    }

    public ll_node getLink() {
        return link;
    }

    public void setLink(ll_node link) {
        this.link = link;
    }

    public node getData() {
        return data;
    }

    public void setData(node data) {
        this.data = data;
    }
   
   
}

class ll_queue
{
    ll_node front , rear;
    int size;
   
    public ll_queue()
    {
        front = rear = null;
    }
   
    public boolean isempty()
    {
        return front==null;
    }
   
    public void enqueue(node n)
    {
        ll_node nptr = new ll_node(n,null);
        if(front==null)
        {
            front = rear = nptr;
        }
        else
        {
            rear.setLink(nptr);
            rear = nptr;
        }
       
    }
   
    public node dequeue()
    {
        if(isempty())
        {
            return null;
        }
        else
        {
            node d = front.getData();
            front = front.getLink();
            return d;
        }
    }
}




 class node
 {
     node left, right ;
     int data;
     public node()
     {
         left = null;
         right = null;
         data = 0;
     }
     public node(int data)
     {
         left = right = null;
         this.data = data;
     }

        public node getLeft() {
            return left;
        }

        public void setLeft(node left) {
            this.left = left;
        }

        public node getRight() {
            return right;
        }

        public void setRight(node right) {
            this.right = right;
        }

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }
   
 }

 class binary_tree
 {
     node root;
     int size;
   
     public binary_tree()
     {
         root = null;
     }
   
     public boolean isEmpty()
     {
         return root==null;
     }
     public void insert(int data)
     {
         root = insert_binary(root,data);
     }
   
     public node insert_binary(node noder,int data)
     {
         if(noder==null)
         {
             noder = new node(data);
           
         }
         else
         {
             if(noder.getLeft()==null)
             {
                 noder.left = insert_binary(noder.left,data);
             }
             else
             {
                 noder.right = insert_binary(noder.right,data);
             }
         }
       
         return noder;
     }
   
     public void max()
     {
         int maxr = find_max_recursion(root);
       
         System.out.println(" max value from recurssion function is  ->" +maxr);
       
         int maxnr = find_max_nonrecursion(root);
       
         System.out.println(" max value from nonrecurssion function is  ->" +maxnr);
       
     }
   
     public int find_max_recursion(node root)
     {
         int root_val,left,right,max=-1;
       
         if(root!=null)
         {
             root_val = root.getData();
             left = find_max_recursion(root.getLeft());
             right = find_max_recursion(root.getRight());
             if(left>right)
                 max = left;
                         else
                 max = right;
             if(root_val>max)
                 max = root_val;
           
         }
       
         return max;
       
     }
     public int find_max_nonrecursion(node root)
     {
         int max = -1;
         node temp;
         ll_queue lq = new ll_queue();
         if(root==null)
             return -1;
         lq.enqueue(root);
         while(!lq.isempty())
         {
             temp = lq.dequeue();
             if(max<temp.getData())
                 max = temp.getData();
             if(temp.getLeft()!=null)
             {
             lq.enqueue(temp.getLeft());
             }
             if(temp.getRight()!=null)
             {
             lq.enqueue(temp.getRight());
             }
           
         }
       
         return max;
       
     }
   
     public void display()
     {
         char c;
         Scanner sc = new Scanner(System.in);
         int data;
         do
         {
             data = sc.nextInt();
             insert(data);
             System.out.println("do you want to insert more");
             c = sc.next().charAt(0);
         }while(c=='Y'||c=='y');
         max();
     }
   
     }

 public class FindMax_binary_tree
 {

    public static void main(String[] args) {
        // TODO code application logic here
        binary_tree br = new binary_tree();
        br.display();
    }
   
}