import java.util.Scanner;
public class tree {
public Scanner in=new Scanner(System.in);
tree l,r;
int data;
tree(int x)
{
data=x;
l=null;
r=null;
}
tree root;
tree()
{
root=null;
}
void create() //creation of the tree
{
int x;
tree a;
System.out.println("Enter data");
x=in.nextInt();
root=new tree(x);
}
void insert() //insertion of new node in tree
{
int x;
tree p,q;
System.out.println("Enter data");
x=in.nextInt();
if (root ==null)
{
System.out.println("first create the root node");
}
else
{
p=q=root;
while(q!=null)
{
p=q;
if( x<p.data)
{
q=p.l;
}
else
{
q=p.r;
}
}
if(x<p.data)
{
q=new tree(x);
p.l=q;
}
else
{
if(x>p.data)
{
q=new tree(x);
p.r=q;
}
}
}
}
void preorder(tree ro) //pre order traversal
{
if(ro!=null)
{
System.out.println(ro.data);
preorder(ro.l);
preorder(ro.r);
}
}
void postorder(tree ro) //post order traversal
{
if(ro!=null)
{
postorder(ro.l);
postorder(ro.r);
System.out.println(ro.data);
}
}
void inorder(tree ro) //in order traversal
{
if(ro!=null)
{
inorder(ro.l);
System.out.println(ro.data);
inorder(ro.r);
}
}
void BFT(tree t) //level wise display
{
que q1=new que() ;
que q2=new que() ;
tree p1,p2;
if(t==null)
{
return;
}
q1.insert(t);
System.out.println("\n");
System.out.println(t.data);
while(!q1.empty())
{
System.out.println("\n");
q2.init();
while(!q1.empty())
{
System.out.println("\n");
p1=q1.delete();
if(p1.l!=null)
{
q2.insert(p1.l);
System.out.println(p1.l.data);
}
if(p1.r!=null)
{
q2.insert(p1.r);
System.out.println(p1.r.data);
}
q1=q2;
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
tree a=new tree();
int ch,f=0;
while(f==0)
{
System.out.println("menu\n");
System.out.println("1.create\n2.insert\n3.inorder\n4.preorder\n5.postorder\n6.BFT\n7.End\n");
System.out.println("Enter your choice:");
ch=a.in.nextInt();
switch(ch)
{
case 1:a.create();
break;
case 2:a.insert();
break;
case 3:System.out.println("Inorder travesal is:");
a.inorder(a.root);
break;
case 4:
System.out.println("Preorder travesral is:");
a.preorder(a.root);
break;
case 5:
System.out.println("Postorder travesral is:");
a.postorder(a.root);
break;
case 6:
System.out.println("Levelwise traversal is:");
a.BFT(a.root);
break;
case 7:f=1;
break;
}
}
// TODO Auto-generated method stub
}
}
class que extends tree //class for the Queue
{
tree[] b=new tree[30];
int r,f;
que()
{
r=f=-1;
}
void init()
{
r=f=-1;
}
boolean empty()
{
if(r==-1)
return true;
return false;
}
void insert(tree p) //insertion of the node in the Queue
{
if(empty())
r=f=0;
else
r=r+1;
b[r]=p;
}
tree delete() //deletion of the node from Queue
{
tree p=b[f];
if(r==f)
{
r=f=-1;
}
else
f=f+1;
return p;
}
}
output:
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
1
Enter data
5
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
2
Enter data
3
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
2
Enter data
6
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
2
Enter data
1
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
2
Enter data
2
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
3
Inorder travesal is:
1
2
3
5
6
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
4
Preorder travesral is:
5
3
1
2
6
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
5
Postorder travesral is:
2
1
3
6
5
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
6
Levelwise traversal is:
5
3
6
1
2
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
public class tree {
public Scanner in=new Scanner(System.in);
tree l,r;
int data;
tree(int x)
{
data=x;
l=null;
r=null;
}
tree root;
tree()
{
root=null;
}
void create() //creation of the tree
{
int x;
tree a;
System.out.println("Enter data");
x=in.nextInt();
root=new tree(x);
}
void insert() //insertion of new node in tree
{
int x;
tree p,q;
System.out.println("Enter data");
x=in.nextInt();
if (root ==null)
{
System.out.println("first create the root node");
}
else
{
p=q=root;
while(q!=null)
{
p=q;
if( x<p.data)
{
q=p.l;
}
else
{
q=p.r;
}
}
if(x<p.data)
{
q=new tree(x);
p.l=q;
}
else
{
if(x>p.data)
{
q=new tree(x);
p.r=q;
}
}
}
}
void preorder(tree ro) //pre order traversal
{
if(ro!=null)
{
System.out.println(ro.data);
preorder(ro.l);
preorder(ro.r);
}
}
void postorder(tree ro) //post order traversal
{
if(ro!=null)
{
postorder(ro.l);
postorder(ro.r);
System.out.println(ro.data);
}
}
void inorder(tree ro) //in order traversal
{
if(ro!=null)
{
inorder(ro.l);
System.out.println(ro.data);
inorder(ro.r);
}
}
void BFT(tree t) //level wise display
{
que q1=new que() ;
que q2=new que() ;
tree p1,p2;
if(t==null)
{
return;
}
q1.insert(t);
System.out.println("\n");
System.out.println(t.data);
while(!q1.empty())
{
System.out.println("\n");
q2.init();
while(!q1.empty())
{
System.out.println("\n");
p1=q1.delete();
if(p1.l!=null)
{
q2.insert(p1.l);
System.out.println(p1.l.data);
}
if(p1.r!=null)
{
q2.insert(p1.r);
System.out.println(p1.r.data);
}
q1=q2;
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
tree a=new tree();
int ch,f=0;
while(f==0)
{
System.out.println("menu\n");
System.out.println("1.create\n2.insert\n3.inorder\n4.preorder\n5.postorder\n6.BFT\n7.End\n");
System.out.println("Enter your choice:");
ch=a.in.nextInt();
switch(ch)
{
case 1:a.create();
break;
case 2:a.insert();
break;
case 3:System.out.println("Inorder travesal is:");
a.inorder(a.root);
break;
case 4:
System.out.println("Preorder travesral is:");
a.preorder(a.root);
break;
case 5:
System.out.println("Postorder travesral is:");
a.postorder(a.root);
break;
case 6:
System.out.println("Levelwise traversal is:");
a.BFT(a.root);
break;
case 7:f=1;
break;
}
}
// TODO Auto-generated method stub
}
}
class que extends tree //class for the Queue
{
tree[] b=new tree[30];
int r,f;
que()
{
r=f=-1;
}
void init()
{
r=f=-1;
}
boolean empty()
{
if(r==-1)
return true;
return false;
}
void insert(tree p) //insertion of the node in the Queue
{
if(empty())
r=f=0;
else
r=r+1;
b[r]=p;
}
tree delete() //deletion of the node from Queue
{
tree p=b[f];
if(r==f)
{
r=f=-1;
}
else
f=f+1;
return p;
}
}
output:
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
1
Enter data
5
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
2
Enter data
3
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
2
Enter data
6
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
2
Enter data
1
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
2
Enter data
2
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
3
Inorder travesal is:
1
2
3
5
6
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
4
Preorder travesral is:
5
3
1
2
6
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
5
Postorder travesral is:
2
1
3
6
5
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
6
Levelwise traversal is:
5
3
6
1
2
menu
1.create
2.insert
3.inorder
4.preorder
5.postorder
6.BFT
7.End
Enter your choice:
No comments:
Post a Comment