AIM
To implement the b-tree with insert
and delete operations using Java.
ALGORITHM
Step
1:
Start the program by defining function.
Step
2:
Declare the class btree
Step
3:
The insert and delete operations are performed
Step
4:
To insert, check if root is empty, if it is empty
insert the element as root.
Step
5:
If it is greater insert it into right sub tree.
Step
6:
Otherwise, insert it into left sub tree
Step
7:
Use the function split, to split the nodes
Step
8:
Call the function display to display
data1,data2,address and
parent
Step
9:
End of the program
PROGRAM
B-TREE
import java.io.*;
class bnode
{
int
data1,data2;
bnode lptr,mptr,rptr,parent;
public void
bnode()
{
this.data1=this.data2=0;
this.lptr=this.mptr=this.rptr=this.parent=null;
}
}
class btree
{
bnode
root=null;
bnode p,p1;
bnode prev;
void
insert(int ele)
{
bnode temp=new bnode();
temp.data1=ele;
if(root==null)
{
root=temp;
}
else
{
p1=root;
while(p1!=null)
{
prev=p1;
if(temp.data1<p1.data1)
p1=p1.lptr;
else if((temp.data1>p1.data1) &&(temp.data1<p1.data2))
p1=p1.mptr;
else
p1=p1.rptr;
}
p1=prev;
while(p1!=null)
{
if(p1.data2==0)
{
if(temp.data1<p1.data1)
{
int
t=p1.data1;
p1.data1=temp.data1;
p1.data2=t;
p1.lptr=temp.lptr;
if(temp.lptr!=null)
temp.lptr.parent=p1;
p1.mptr=temp.rptr;
if(temp.rptr!=null)
temp.rptr.parent=p1;
}
else
{
p1.data2=temp.data1;
p1.mptr=temp.lptr;
if(temp.lptr!=null)
temp.lptr.parent=p1;
p1.rptr=temp.rptr;
if(temp.rptr!=null)
temp.rptr.parent=p1;
}
temp.parent=p1.parent;
break;
}
else
if((p1.data1!=0) && (p1.data2!=0))
{
p1=split(temp,p1);
temp=p1;
p1=p1.parent;
}
}
}
display(root);
}