B树的插入

//这是一个奇数阶b树
#include<iostream.h>

#define m 5
typedef struct nd
{
 int keynum;
 int data[m+1];
 struct nd *chd[m+1];
 struct nd *pr;
}btnode,*pbtnode;

class btree
{
private:
 pbtnode root;
public:
 btree()
 {
  root=0;
  int i;
  cout<<"enter node infor ,0 is the endl/nstart:";
//  cin>>i;
//  while(i)
//  {
   for(i=111;i>0;i--)
  // for(i=1;i<1111;i++)
   {
    if(i==10)
     call();
   InsertBtree(root,i);
   }
   
//   cin>>i;
//  }
 }

 void call()
 {
  return;
 }

 int search(pbtnode &rt,int k)
 {
  if(rt==0)return 0;
  for(int i=1;i<=rt->keynum&&rt->data[i]<k;i++);
  return i;
 }

 void searchBtree(pbtnode &rt,int k,pbtnode &q,bool &bl)
 {
  while(rt)
  {
   q=rt;
   int i=search(rt,k);
   if(i==0)
   {
    cout<<"have a null tear!/n";
    return;
   }
   if(rt->data[i]==k)
   {
    cout<<"the node have in the tree!/n";
    bl=true;
    return;
   }
   //*****
   else if(rt->keynum<i)
   {
    rt=rt->chd[i-1];
   }
   //*************
   else if(k<rt->data[i])
   {
    rt=rt->chd[i-1];
   }
//   else
//    rt=rt->chd[i];
   bl=false;
  }
 }

 //Insert the information in the tree!1

 void Insert(pbtnode &rt,int k,pbtnode ap)
 {
  if(!rt)
  {
   rt=new btnode;
   rt->data[1]=k;
   for(int i=0;i<=m;i++)
    rt->chd[i]=0;
   rt->keynum=1;
   rt->pr=0;
   cout<<"seceess insert!/n";
  }
  else
  {
   int i=search(rt,k);
   if(rt->data[i]==k)
   {
    cout<<"the node have in the tree!/n";
   }
   else
   {
    for(int j=rt->keynum;j>=i;j--)
    {
     rt->data[j+1]=rt->data[j];
    }
    for(j=rt->keynum;j>=i;j--)
    {
     rt->chd[j+1]=rt->chd[j];
    }
    rt->chd[i]=ap;
    rt->data[i]=k;
    rt->keynum++;
    cout<<"Insert seccess!!/n";
    if(rt->keynum==m)
    {
     slpit(rt);
    }
   }
  }
 }

 void InsertBtree(pbtnode &rt,int k)
 {
  pbtnode p=rt,q;
  bool bl;
  if(!rt)
  {
   Insert(rt,k,0);
   return;
  }
  else
  {
   searchBtree(p,k,q,bl);
   Insert(q,k,0);
  }
 }

 //*****************************************************************************
 void slpit(pbtnode &rt)
 {
  if(!rt->pr)
  {
   newroot(root);
  }
  else
  {
   btnode q=*rt;        
   int i,j;
   pbtnode p=new btnode;
 
   for(j=0, i=m/2+1;i<=m;i++,j++)  
   {       
    p->chd[j]=q.chd[i];
    if(p->chd[j]!=0)      
     p->chd[j]->pr=p;
   }
   
   for(i=1,j=m/2+2;j<=m;j++,i++)
   {
    p->data[i]=q.data[j];       //change in the here!!
   }
   rt->keynum=m/2;        
   p->keynum=m/2;
   p->pr=rt->pr;
//   for(i=0;i<=p->keynum;i++) //////
//   {
//    if(p->chd[i])
//     p->chd[i]=p;
//   }
   int n=rt->data[m/2+1];
   Insert(q.pr,n,p);
  }
  cout<<"insert up seccess!/n";
 }
 //*****************************************************************************

 void newroot(pbtnode &rt)        //change at the night
 {
  pbtnode root=new btnode;
  pbtnode nd=new btnode;
  for(int i=0;i<m+1;i++)
  {
   root->chd[i]=0;
   nd->chd[i]=0;
  }
  //add
  int j;
  for(i=0,j=m/2+1;j<=m+1;j++,i++)
  {     //**
   if(rt->chd[j])                   //add in the night
    rt->chd[j]->pr=nd;
   nd->chd[i]=rt->chd[j];
  }

 
  root->pr=0;                          
  root->keynum=1;
  root->data[1]=rt->data[m/2+1];
  root->chd[0]=rt;
  root->chd[1]=nd;
  rt->pr=root;
  nd->pr=root;

  for(i=m/2+2,j=1;i<=m;i++,j++)
  {
   nd->data[j]=rt->data[i];
      
  }
  nd->chd[j]=rt->chd[i];
//  nd->chd[j]->pr=nd;
  rt->keynum=m/2;
  nd->keynum=m-rt->keynum-1;
  rt=root;
  //****************************************
 }

 //delete a key from the tree


 void Inorder(pbtnode &rt)
 {
  if(!rt)return;
  for(int i=0;i<=rt->keynum;i++)
  {
   Inorder(rt->chd[i]);
   if(i<rt->keynum)
    cout<<rt->data[i+1]<<"   ";
  }
 }

 void print()
 {
  Inorder(root);
  cout<<"/nthe B_tree/n";
 }

};

void main()
{
 btree bt;
 bt.print();

原文地址:https://www.cnblogs.com/thubier/p/11944287.html