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()
{
LVR(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);
}
}
void del(int a)
{
t=del(t,a);
}
tree del(tree t, int a)
{
tree temp;
if(t==null)
System.out.println("Element not Found");
else if(a
t.left = del(t.left,a);
else if(a>t.x)
t.right = del(t.right,a);
else if (t.left ==null && t.right ==null)
t=null;
else
System.out.println("Not a leaf Node");
return t;
}
}
class TreeDel
{
public static void main(String as[])
{
BTree t1=new BTree();
t1.insert(30);
t1.insert(20);
t1.insert(10);
t1.insert(40);
t1.insert(60);
t1.showLVR();
t1.del(10);
System.out.println("\nAfter Deleting\n");
t1.showLVR();
}
}
/* Output */
10 20 30 40 60
After Deleting
20 30 40 60
0 comments:
Post a Comment