主题:高手给我的函数加注释...关于B树的节点合并的
BTree Unite(BTree T,BTree p,BTree y) /* 结点合并 */
{BTree t,l,ap,last;
int u,v,w,j,a,finish,s,i;
Book x;
t=p->parent;
u=1+p->keynum;
if(t->keynum==0)
{T=y;T->parent->keynum=0;} /* 如果父亲结点为空,则将合并后的结点改为根结点 */
else
{if(p->keynum!=0)
v=Search(t,p->key[p->keynum].key);
else
v=Search(t,y->key[y->keynum].key);
if(v!=0)
{l=t->ptr[v-1];
w=l->keynum;
l->key[l->keynum+1]=t->key[v];
l->keynum+=1;
for(j=l->keynum+1;j<=l->keynum+p->keynum;j++)
l->key[j]=p->key[j-l->keynum];
l->keynum=l->keynum+p->keynum;
for(j=w+1;j<=w+u;j++)
{l->ptr[j]=y;
y->parent=l;
l->ptr[j]->parent=l;}
for(j=v;j<t->keynum;j++)
{t->key[j]=t->key[j+1];
t->ptr[j]=t->ptr[j+1];
t->ptr[j]->parent=t;}
t->keynum-=1;
if(l->keynum>=m)
{finish=FALSE;
ap=(BTree)malloc(sizeof(BTNode));
ap->parent=NULL;
ap->keynum=0;
while(l->keynum!=0&&finish==FALSE)
{s=(m+1)/2;
x=l->key[s];
ap=split(l,ap);
last=l;
l=l->parent;
if(l->keynum!=0)
{i=Search(l,x.key);
l=Insert(l,i,x,ap);}
if(l->keynum<m&&l->keynum!=0)
{finish=TRUE;
l=last;}
}
if(finish==FALSE)
{T=Newroot(last,x,ap);l=T;}}
if(t->keynum==0) T=Unite(T,t,l);}
else
{l=t->ptr[1];
for(j=l->keynum+1+p->keynum;j>1+p->keynum;j--)
{l->key[j]=l->key[j-1-p->keynum];
l->ptr[j]=l->ptr[j-1-p->keynum];
l->ptr[j]->parent=l;}
l->ptr[1+p->keynum]=l->ptr[0];
l->ptr[1+p->keynum]->parent=l;
for(j=1;j<=p->keynum;j++)
l->key[j]=p->key[j];
l->key[1+p->keynum]=t->key[1];
for(j=0;j<1+p->keynum;j++)
{l->ptr[j]=y;
y->parent=l;
l->ptr[j]->parent=l;}
l->keynum=l->keynum+1+p->keynum;
for(j=1;j<t->keynum;j++)
{t->key[j]=t->key[j+1];
t->ptr[j]=t->ptr[j+1];
t->ptr[j]->parent=t;}
t->keynum-=1;
if(l->keynum>=m)
{finish=FALSE;
ap=(BTree)malloc(sizeof(BTNode));
ap->parent=NULL;
ap->keynum=0;
while(l->keynum!=0&&finish==FALSE)
{s=(m+1)/2;
x=l->key[s];
ap=split(l,ap);
last=l;
l=l->parent;
if(l->keynum!=0)
{i=Search(l,x.key);
l=Insert(l,i,x,ap);}
if(l->keynum<m&&l->keynum!=0)
{finish=TRUE;
l=last;}
}
if(finish==FALSE)
{T=Newroot(last,x,ap);l=T;}
}
if(t->keynum==0)
T=Unite(T,t,l);
else
{t->ptr[0]=l;
t->ptr[0]->parent=t;}
}
}
return(T);
}
先谢谢了..从别的地方拷的,完全看不懂的说,请高手在关键的地方给加点注释.急用..再次感谢..
{BTree t,l,ap,last;
int u,v,w,j,a,finish,s,i;
Book x;
t=p->parent;
u=1+p->keynum;
if(t->keynum==0)
{T=y;T->parent->keynum=0;} /* 如果父亲结点为空,则将合并后的结点改为根结点 */
else
{if(p->keynum!=0)
v=Search(t,p->key[p->keynum].key);
else
v=Search(t,y->key[y->keynum].key);
if(v!=0)
{l=t->ptr[v-1];
w=l->keynum;
l->key[l->keynum+1]=t->key[v];
l->keynum+=1;
for(j=l->keynum+1;j<=l->keynum+p->keynum;j++)
l->key[j]=p->key[j-l->keynum];
l->keynum=l->keynum+p->keynum;
for(j=w+1;j<=w+u;j++)
{l->ptr[j]=y;
y->parent=l;
l->ptr[j]->parent=l;}
for(j=v;j<t->keynum;j++)
{t->key[j]=t->key[j+1];
t->ptr[j]=t->ptr[j+1];
t->ptr[j]->parent=t;}
t->keynum-=1;
if(l->keynum>=m)
{finish=FALSE;
ap=(BTree)malloc(sizeof(BTNode));
ap->parent=NULL;
ap->keynum=0;
while(l->keynum!=0&&finish==FALSE)
{s=(m+1)/2;
x=l->key[s];
ap=split(l,ap);
last=l;
l=l->parent;
if(l->keynum!=0)
{i=Search(l,x.key);
l=Insert(l,i,x,ap);}
if(l->keynum<m&&l->keynum!=0)
{finish=TRUE;
l=last;}
}
if(finish==FALSE)
{T=Newroot(last,x,ap);l=T;}}
if(t->keynum==0) T=Unite(T,t,l);}
else
{l=t->ptr[1];
for(j=l->keynum+1+p->keynum;j>1+p->keynum;j--)
{l->key[j]=l->key[j-1-p->keynum];
l->ptr[j]=l->ptr[j-1-p->keynum];
l->ptr[j]->parent=l;}
l->ptr[1+p->keynum]=l->ptr[0];
l->ptr[1+p->keynum]->parent=l;
for(j=1;j<=p->keynum;j++)
l->key[j]=p->key[j];
l->key[1+p->keynum]=t->key[1];
for(j=0;j<1+p->keynum;j++)
{l->ptr[j]=y;
y->parent=l;
l->ptr[j]->parent=l;}
l->keynum=l->keynum+1+p->keynum;
for(j=1;j<t->keynum;j++)
{t->key[j]=t->key[j+1];
t->ptr[j]=t->ptr[j+1];
t->ptr[j]->parent=t;}
t->keynum-=1;
if(l->keynum>=m)
{finish=FALSE;
ap=(BTree)malloc(sizeof(BTNode));
ap->parent=NULL;
ap->keynum=0;
while(l->keynum!=0&&finish==FALSE)
{s=(m+1)/2;
x=l->key[s];
ap=split(l,ap);
last=l;
l=l->parent;
if(l->keynum!=0)
{i=Search(l,x.key);
l=Insert(l,i,x,ap);}
if(l->keynum<m&&l->keynum!=0)
{finish=TRUE;
l=last;}
}
if(finish==FALSE)
{T=Newroot(last,x,ap);l=T;}
}
if(t->keynum==0)
T=Unite(T,t,l);
else
{t->ptr[0]=l;
t->ptr[0]->parent=t;}
}
}
return(T);
}
先谢谢了..从别的地方拷的,完全看不懂的说,请高手在关键的地方给加点注释.急用..再次感谢..