class tree
{
int x;
tree left;
tree right;
}
class BTree
{
private tree t;
BTree()
{
t=null;
}
public void insert(int v)
{
t=insert(t,v);
}
public void showLVR()
{
System.out.println("\nLVR :\n");
LVR(t);
}
public void showVLR()
{
System.out.println("\nVLR :\n");
VLR(t);
}
public void showLRV()
{
System.out.println("\nLRV :\n");
LRV(t);
}
tree insert(tree t, int v)
{
if(t==null)
{
t = new tree();
t.x = v;
t.left = null;
t.right = null;
}
else if(v
t.left = insert(t.left,v);
else if(v>t.x)
t.right = insert(t.right,v);
return t;
}
public void LVR(tree t)
{
if(t!=null)
{
LVR(t.left);
System.out.print("\t"+t.x);;
LVR(t.right);
}
}
public void LRV(tree t)
{
if(t!=null)
{
LRV(t.left);
LRV(t.right);
System.out.print("\t"+t.x);
}
}
public void VLR(tree t)
{
if(t!=null)
{
System.out.print("\t"+t.x);
VLR(t.left);
VLR(t.right);
}
}
}
class BTreeTest
{
public static void main(String as[])
{
BTree t1=new BTree();
t1.insert(30);
t1.insert(20);
t1.insert(10);
t1.insert(40);
t1.showLVR();
t1.showVLR();
t1.showLRV();
}
}
/* Output */
LVR :
10 20 30 40
VLR :
30 20 10 40
LRV :
10 20 40 30
0 comments:
Post a Comment