Escolar Documentos
Profissional Documentos
Cultura Documentos
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;
}
}
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: