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();
    }
   
}

No comments:

Post a Comment