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
    }
   
}

No comments:

Post a Comment