Search This Blog

Saturday, 22 November 2014

JAVA Code:Query Operation In Binary Search Tree (BST)

/* Query operation in BST
*  1>Search
*  2>insert
*  3>delete
*  4>successor
*  5>predecessor
*  6>Tree Minimum
*  7>Tree Maximum
*  8> Tree travelsals- inorder,preorder,levelorder,postorder
***
*author...maulik gohil
*/

import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
Qprocess qp=new Qprocess();

boolean flag=true;

while(flag)
{
System.out.println();
System.out.println("0-exit 1-insert 2-search 3-delete 4-successor 5-predecessor 6-                                                                  maximum 7-minimum");

int n=Integer.parseInt(s.nextLine());
if(n==1)
{
//insert
int entry=Integer.parseInt(s.nextLine());
Node node=new Node(entry);
qp.insert(node);
}
else if(n==2)
{
//search
int entry=Integer.parseInt(s.nextLine());
qp.search(entry);
}
else if(n==3)
{
//delete
int entry=Integer.parseInt(s.nextLine());
qp.delete(entry);
}
else if(n==4)
{
//successor
int entry=Integer.parseInt(s.nextLine());
qp.successor(entry);

}
else if(n==5)
{
//predecessor
 int entry=Integer.parseInt(s.nextLine());
qp.predecessor(entry);
}
else if(n==6)
{
//maximum
qp.maximum();
}
else if(n==7)
{
//minimum
qp.minimum();
}
if(n==0)
{
//termination of loop
flag=false;
}
}
        }
}


public class Qprocess {
public Node root=null;
public void insert(Node node)
{
Node y=null;
Node x=null;
x=root;
while(x!=null)
{
y=x;
if(node.key<x.key)
x=x.left;
else
x=x.right;
}
node.parent=y;
if(y==null)
root=node;
else if(node.key<y.key)
y.left=node;
else
y.right=node;
System.out.print("inorder  "+">> ");
this.Inorder(root); 
System.out.println();
System.out.print("preorder "+">> ");
this.Preorder(root);
System.out.println();
System.out.print("postorder"+">> ");
this.Postorder(root);
System.out.println();
System.out.print("levelorder"+">> ");
this.levelorder(root);
}
private void Inorder(Node x)
{
if(x==null)
return;
this.Inorder(x.left);
System.out.print(" "+x.key);
this.Inorder(x.right);
}
private void Preorder(Node x)
{
if(x==null)
return;
System.out.print(" "+x.key);
this.Preorder(x.left);
this.Preorder(x.right);
}
private void Postorder(Node x)
{
if(x==null)
return;
this.Postorder(x.left);
this.Postorder(x.right);
System.out.print(" "+x.key);
}
private void levelorder(Node x)
{
Queue queue=new LinkedList();
if(x==null)
return;
else
queue.add(x);
while(!queue.isEmpty())
{
Node current=(Node) queue.poll();
System.out.print(" "+current.key);
if(current.left!=null)
queue.add(current.left);
if(current.right!=null)
queue.add(current.right);
}
}
public void search(int x)
{
Node temp=root;
while(temp!=null && temp.key!=x)
{
if(x<temp.key)
temp=temp.left;
else
temp=temp.right;
}
if(temp!=null)
System.out.print(temp.key);
else
System.out.print("not found");
}
public void maximum()
{
Node temp=root;
while(temp.right!=null)
{
temp=temp.right;
}
System.out.print(temp.key);
}

public void minimum()
{
Node temp=root;
while(temp.left!=null)
{
temp=temp.left;
}
System.out.print(temp.key);
}

public void successor(int x) 
{
Node temp=root;
//search method to reach the node where key value match
while(temp!=null && temp.key!=x)
{
if(x<temp.key)
temp=temp.left;
else
temp=temp.right;
}
//successor code
if(temp.right!=null)
{
temp=temp.right;
//treeMIN
while(temp.left!=null)
{
temp=temp.left;
}
System.out.print(temp.key);
}
else
{
Node p=temp.parent;
while(p!=null && p.right==temp)
{
temp=p;
p=p.parent;
}
System.out.print(p.key);
}
}
public void predecessor(int x) 
{
Node temp=root;
//search method to reach the node where key value match
while(temp!=null && temp.key!=x)
{
if(x<temp.key)
temp=temp.left;
else
temp=temp.right;
}
//predecessor code
if(temp.left!=null)
{
temp=temp.left;
//treeMAX
while(temp.right!=null)
{
temp=temp.right;
}
System.out.print(temp.key);
}
else
{
Node p=temp.parent;
while(p!=null && p.left==temp)
{
temp=p;
p=p.parent;
}
System.out.print(p.key);
}
}
        public void delete(int x) 
{
Node t=null;
Node y=null;
Node temp=root;
while(temp!=null && temp.key!=x)
{
if(x<temp.key)
temp=temp.left;
else
temp=temp.right;
}
if(temp.left==null || temp.right==null)//one or no child
{
y=temp;
}
else
{
if(temp.right!=null)
{
temp=temp.right;
//treeMIN
while(temp.left!=null)
{
temp=temp.left;
}
y=temp;
System.out.println(y.key);
System.out.println("check22");
}
else
{
Node p=temp.parent;
while(p!=null && p.right==temp)
{
temp=p;
p=p.parent;
}
y=p;
}
}
if(y.left!=null)
{
t=y.left;
}
else
{
t=y.right;
}
if(t!=null)
{
t.parent=y.parent;
}
if(y.parent==null)
{
root=t;
}
else if(y==y.parent.left)
{
y.parent.left=t;
}
else
{
y.parent.right=t;
}
System.out.print("inorder  "+">> ");
this.Inorder(root); 
System.out.println();
System.out.print("preorder "+">> ");
this.Preorder(root);
System.out.println();
System.out.print("postorder"+">> ");
this.Postorder(root);
System.out.println();
System.out.print("levelorder"+">> ");
this.levelorder(root);
}
}
public class Node 
{
int key;
Node left;
Node right;
Node parent;
public Node(int x)
{
parent=null;
left=null;
right=null;
this.key=x;
}
}

No comments:

Post a Comment

Blog Archive