void main() { BTNode *t=NULL; Result s; int j,n=10; KeyType a[]={4,9,0,1,8,6,3,5,2,7},k; m=3; //3阶B-树 Max=m-1;Min=(m-1)/2; printf("创建一棵%d阶B-树:\n",m); for (j=0;j<n;j++) //创建一棵3阶B-树t { s=SearchBTree(t,a[j]); if (s.tag==0) InsertBTree(t,a[j],s.pt,s.i); printf(" 第%d步,插入%d: ",j+1,a[j]);DispBTree(t);printf("\n"); } printf(" 结果B-树: ");DispBTree(t);printf("\n"); printf("删除操作:\n"); k=8; DeleteBTree(k,t); printf(" 删除%d: ",k); printf("B-树: ");DispBTree(t);printf("\n"); k=1; DeleteBTree(k,t); printf(" 删除%d: ",k); printf("B-树: ");DispBTree(t);printf("\n"); }
void main() { BTNode *t=NULL; Result s; int j,n=20; KeyType a[]={1,2,6,7,11,4,8,13,10,5,17,9,16,20,3,12,14,18,19,15},k; m=5; /*例10.7*/ Max=m-1;Min=(m-1)/2; printf("\n"); printf(" 创建一棵%d阶B-树:\n",m); for (j=0;j<n;j++) /*创建一棵5阶B-树t*/ { s=SearchBTree(t,a[j]); if (s.tag==0) InsertBTree(t,a[j],s.pt,s.i); printf(" 第%d步,插入%d: ",j+1,a[j]);DispBTree(t);printf("\n"); } printf(" 删除操作:\n"); /*例10.8*/ k=8; DeleteBTree(k,t); printf(" 删除%d: ",k); DispBTree(t);printf("\n"); k=16; DeleteBTree(k,t); printf(" 删除%d: ",k); DispBTree(t);printf("\n"); k=15; DeleteBTree(k,t); printf(" 删除%d: ",k); DispBTree(t);printf("\n"); k=4; DeleteBTree(k,t); printf(" 删除%d: ",k); DispBTree(t);printf("\n\n"); }
int main(){ int i=0; srand(time(NULL)); BTNode* root=RBinit(); for(i=0;i<N;i++) root=RBinsert(root,rand()%100); printf("DispBTree:\n"); DispBTree(root); return 0; }
void DispBTree(BTNode *t) //以括号表示法输出B-树 { int i; if (t!=NULL) { printf("["); //输出当前结点关键字 for (i=1;i<t->keynum;i++) printf("%d ",t->key[i]); printf("%d",t->key[i]); printf("]"); if (t->keynum>0) { if (t->ptr[0]!=0) printf("("); //至少有一个子树时输出"("号 for (i=0;i<t->keynum;i++) //对每个子树进行递归调用 { DispBTree(t->ptr[i]); if (t->ptr[i+1]!=NULL) printf(","); } DispBTree(t->ptr[t->keynum]); if (t->ptr[0]!=0) printf(")"); //至少有一个子树时输出")"号 } } }
void DispBTree(BTNode *t) /*以括号表示法输出B-树*/ { int i; if (t!=NULL) { printf("["); /*输出当前结点关键字*/ for (i=1;i<t->keynum;i++) printf("%d ",t->key[i]); printf("%d",t->key[i]); printf("]"); if (t->keynum>0) { if (t->ptr[0]!=0) printf("("); /*至少有一个子树时输出"("号*/ for (i=0;i<t->keynum;i++) /*对每个子树进行递归调用*/ { DispBTree(t->ptr[i]); if (t->ptr[i+1]!=NULL) printf(","); } DispBTree(t->ptr[t->keynum]); if (t->ptr[0]!=0) printf(")"); /*至少有一个子树时输出")"号*/ } } }