Você está na página 1de 14

//SOUVIK & SOUMODIP

A program to implement a binary tree that can insert,delete,display &


search elements .

import java.io.*;
class TreeNode
{
public TreeNode left,right;
public int data;
public TreeNode()
{
data=0;
left=right=null;
}
public TreeNode(int n)
{
data=n;
left=right=null;
}
public void disp()
{
System.out.print(data+" ");
}
public void setLeft(TreeNode n)
{
left=n;
}
public void setRight(TreeNode n)
{
right=n;
}
public TreeNode getLeft()
{
return left;
}
public TreeNode getRight()
{
return right;
}
public void setData(int d)
{
data=d;
}
public int getData()
{
return data;
}
}

public class BinaryTree


{
TreeNode root;
public BinaryTree()
{
root=null;
}
public void insert(int data)
{
root=insert(root,data);
}
public boolean isEmpty()
{
return root==null;
}
public TreeNode insert(TreeNode node,int data)
{
if(node==null)
node=new TreeNode(data);
else
{
if(data<=node.data)
node.left=insert(node.left,data);
else
node.right=insert(node.right,data);
}
return node;
}
public void Delete(int key)
{
if(isEmpty()==true)
System.out.println("Tree empty");
else if(Search(key)==false)
System.out.println("Sorry! "+key+" is not found in tree!");
else
{
root=Delete(root,key);
System.out.println(key+" deleted from the tree");
}
}
public TreeNode Delete(TreeNode root,int key)
{
TreeNode p,p2,n;
if(root.getData()==key){
TreeNode lt,rt;
lt=root.getLeft();
rt=root.getRight();
if(lt==null && rt==null)
return null;
else if(lt==null)
{
p=rt;
return p;
}
else if(rt==null)
{
p=lt;
return p;
}
else
{
p2=rt;
p=rt;
while(p.getLeft()!=null)
p=p.getLeft();
p.setLeft(lt);
return p2;
}
}
if(key<root.getData())
{
n=Delete(root.getRight(),key);
root.setLeft(n);
}
else
{
n=Delete(root.getRight(),key);
root.setRight(n);
}
return root;
}
public int countNodes()
{
return countNodes(root);
}
public int countNodes(TreeNode r)
{
if(r==null)
return 0;
else
{
int l=1;
l=l+countNodes(r.getLeft());
l=l+countNodes(r.getRight());
return l;
}
}
public boolean Search(int value)
{
return(Search(root,value));
}
public boolean Search(TreeNode r,int val)
{
boolean Found=false;
while((r!=null) && !Found)
{
int rval=r.getData();
if(val<rval)
r=r.getLeft();
else if(val>rval)
r=r.getRight();
else
{
Found=true;
break;
}
Found=Search(r,val);
}
return Found;
}
public void inorder()
{
inorder(root);
}
public void inorder(TreeNode r)
{
if(r!=null)
{
inorder(r.getLeft());
System.out.print(r.getData()+" ");
inorder(r.getRight());
}
}
public void preorder()
{
preorder(root);
}
public void preorder(TreeNode r)
{
if(r!=null)
{
System.out.print(r.getData()+" ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
public void postorder()
{
postorder(root);
}
public void postorder(TreeNode r)
{
if(r!=null)
{
postorder(r.getLeft());
postorder(r.getRight());
System.out.println(r.getData()+" ");
}
}
}

import java.io.*;
public class use_binary_tree
{
public static BinaryTree bt;
public static void main()throws IOException
{
bt=new BinaryTree();
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
while(true)
{
System.out.println("Enter your choice");
System.out.println("Enter 1. to insert");
System.out.println("Enter 2. to delete");
System.out.println("Enter 3. to search");
System.out.println("Enter 4. to display the size");
System.out.println("Enter 5. to display the nodes");
System.out.println("Enter 6. to exit");
int ch=Integer.parseInt(br.readLine());
if(ch==1)
{
System.out.print("Enter a number: ");
int n=Integer.parseInt(br.readLine());
bt.insert(n);
}
else if(ch==2)
{
System.out.print("Enter the number to be deleted: ");
int n=Integer.parseInt(br.readLine());
bt.Delete(n);
}
else if(ch==3)
{
System.out.print("Enter the number to be deleted: ");
int n=Integer.parseInt(br.readLine());
if(bt.Search(n)==true)
System.out.println("The element "+n+" is found");
else
System.out.println("Search unsuccessfull!!!");
}
else if(ch==4)
System.out.println("The no. of nodes is "+bt.countNodes());
else if(ch==5)
{
System.out.println("Enter your choice");
System.out.println("Enter 1. to display in inorder");
System.out.println("Enter 2. to display in preorder");
System.out.println("Enter 3. to display in postorder");
int c=Integer.parseInt(br.readLine());
if(c==1)
bt.inorder();
else if(c==2)
bt.preorder();
else if(c==3)
bt.postorder();
else
break;
}
else if(ch==6)
break;
else
System.out.println("You have entered a invalid input!!!");
}
}
}
SAMPLE OUTPUT:

Enter your choice


Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
1
Enter a number: 2
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
1
Enter a number: 3
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
1
Enter a number: 6
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
1
Enter a number: 5
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
1
Enter a number: 7
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
1
Enter a number: 8
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
1
Enter a number: 9
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
2
Enter the number to be deleted: 7
7 deleted from the tree
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
3
Enter the number to be searched: 9
The element 9 is found
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
4
The no. of nodes is 6
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
5
Enter your choice
Enter 1. to display in inorder
Enter 2. to display in preorder
Enter 3. to display in postorder
1
235689
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
5
Enter your choice
Enter 1. to display in inorder
Enter 2. to display in preorder
Enter 3. to display in postorder
2
236589
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
5
Enter your choice
Enter 1. to display in inorder
Enter 2. to display in preorder
Enter 3. to display in postorder
3598632
Enter your choice
Enter 1. to insert
Enter 2. to delete
Enter 3. to search
Enter 4. to display the size
Enter 5. to display the nodes
Enter 6. to exit
6

Você também pode gostar