zoukankan      html  css  js  c++  java
  • 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=100;i>0;i--)
      // for(i=1;i<9;i++)
       {
        if(i==12)
         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++);          //change for me
      return i;
     }

     void searchBtree(pbtnode &rt,int k,pbtnode &q,bool &bl)
     {
      while(rt)
      {
       q=rt;
    //   int i;
       int i=search(rt,k);   //how to change the deled?whow can call me?
       //damation hvae in the choose
    //      if(i>rt->keynum)
    //    i=i-1;
       if(i==0)
       {
    //    cout<<"have a null tear!/n";
        return;
       }
       if(rt->keynum<i)              //do the change for
       {
        rt=rt->chd[i-1];
       }
       else if(rt->data[i]==k)
       {
    //    cout<<"the node have in the tree!/n";
        bl=true;
        return;
       }
       //*****
      
       //*************
       else if(k<rt->data[i])
       {
        rt=rt->chd[i-1];
       }
       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;
       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];
      rt->keynum=m/2;
      nd->keynum=m-rt->keynum-1;
      rt=root;
      //****************************************
     }

     //delete a key from the tree
     void del(int k)                     //do the 2-3 tree ,use this think to do the programm
     {
      deletebtree(root,k);
     }

     void deletebtree(pbtnode &rt,int k)
     {
      bool bl=false;
      pbtnode p,q=rt;
      searchBtree(q,k,p,bl);
      if(!bl)
      {
       cout<<"the node null in the tree!/n";
       return;
      }
      else
      {
       int i=search(p,k);
       if(!p->chd[i-1])        //whether have a dubeg for here!!
       {
        delnode(p,k);
       }
       else
       {
        int i=search(p,k);
        if(i<=p->keynum&&p->data[i]==k)        //hve dan change 11:12
         i=i;
        else
         i=i-1;
        pbtnode q,pp=p;
        p=p->chd[i];
        while(p)
        {
         q=p;
         p=p->chd[0];
        }
        int n=q->data[1];
        pp->data[i]=n;
        delnode(q,n);
       }
      }
     }

     void delnode(pbtnode &p,int k)
     {
      if(p->keynum>m/2)
      {
       int n=search(p,k);
       for(int i=n;i<p->keynum;i++)
       {
        p->data[i]=p->data[i+1];
        p->chd[i]=p->chd[i+1];
       }
       p->keynum--;
      }
      else
      {
       if(p->pr==0)
       {
        int n=search(p,k);
        for(int i=n;i<p->keynum;i++)
        {
         p->data[i]=p->data[i+1];
         p->chd[i]=p->chd[i+1];
        }
        p->keynum--;
        if(p->keynum==0)
        {
         for(i=0;i<=m;i++)
         {
          if(p->chd[i])
          p->chd[i]->pr=0;
         }
        // p=p->chd[0];
         root=p->chd[0];

        }
       }
       else
       {
        int j;
        pbtnode pr=p->pr,lb,rb;
    //    int j=search(pr,k)-1;
    //    /*
        j=search(pr,k);
        if(pr->data[j]!=k)
        j=j-1;
    //    */
        if(j==0)
         lb=0;
        else
         lb=pr->chd[j-1];
        if(j==pr->keynum)
         rb=0;
        else   
         rb=pr->chd[j+1];
        if(lb && (lb->keynum>m/2))
         lfbr(lb,pr,p,k);
        else if(rb && (rb->keynum>m/2))
         rtbr(p,pr,rb,k);
        else if(lb)
         lfconb(lb,pr,p,k);
        else if(rb)
         rtconb(p,pr,rb,k);
       }
      }
     }

     void lfbr(pbtnode &lb,pbtnode &pr,pbtnode &p ,int k)
     {
      int loc1=search(p,k);    //the k in the node location
      int loc2=search(pr,k)-1;   //the k may in the pr location
      //for the node p do the change .let the node have in the
      for(int i=loc1;i>=1;i--)
      {
       if(i>1)
       p->data[i]=p->data[i-1];
       if(p->chd[i-1])
        p->chd[i]=p->chd[i-1];
      }
      p->data[1]=pr->data[loc2];
      pr->data[loc2]=lb->data[lb->keynum];
      if(lb->chd[lb->keynum])
       lb->chd[lb->keynum]->pr=p;
      p->chd[0]=lb->chd[lb->keynum];
      lb->keynum--;
     }

     void lfconb(pbtnode &lb,pbtnode &pr,pbtnode &p ,int k)
     {
      int j;
      int loc1=search(p,k);    //the k in the node location
    //  int loc2=search(pr,k)-1;   //the k may in the pr location

    //  /*
      int loc2=search(pr,k);               //this the change of you will do
      if(pr->data[loc2]!=k)
       loc2=loc2-1;
    //  */
      //do the change for the node ,I am put the node's right child make null,so I will do like:
      for(int i=loc1;i<p->keynum;i++)
      {
       p->data[i]=p->data[i+1];
       p->chd[i]=p->chd[i+1];
      }   
      lb->data[lb->keynum+1]=pr->data[loc2];
      lb->chd[lb->keynum +1]=p->chd[0];
      if(p->chd[0])
      p->chd[0]->pr=lb;
      pr->chd[loc2]=0;                 //make null
      for(i=lb->keynum+2,j=1; i<=lb->keynum+p->keynum;i++,j++)
      {
       if(p->chd[j])
        p->chd[j]->pr=lb;
       lb->chd[i]=p->chd[j];
       lb->data[i]=p->data[j];
      }
      lb->keynum+=p->keynum;
      delnode(pr,pr->data[loc2]);
     }

     void rtbr(pbtnode &p,pbtnode &pr,pbtnode &rb,int k)
     {
      int loc1=search(p,k);    //the k in the node location
      int loc2=search(pr,k);   //the k may in the pr location,this is a not like for left change
      //change the node to can be insert
      for(int i=loc1;i<p->keynum;i++)
      {
       p->data[i]=p->data[i+1];
       p->chd[i]=p->chd[i+1];
      }
      p->chd[p->keynum]=rb->chd[0];
      if(rb->chd[0])
       rb->chd[0]->pr=p;
      p->data[p->keynum]=pr->data[loc2];
      pr->data[loc2]=rb->data[1];
      rb->chd[0]=rb->chd[1];
      for(i=1;i<rb->keynum;i++)
      {
       rb->chd[i]=rb->chd[i+1];
       rb->data[i]=rb->data[i+1];
      }
      rb->keynum--;
     }

     void rtconb(pbtnode &p,pbtnode &pr,pbtnode &rb,int k)
     {
      int i,j;
      int loc1=search(p,k);    //the k in the node location
      int loc2=search(pr,k);   //the k may in the pr location,this is a not like for lfconb change
      //change the node
      for(i=loc1;i<p->keynum;i++)
      {
       p->chd[i]=p->chd[i+1];
       p->data[i]=p->data[i+1];
      }
      p->data[p->keynum]=pr->data[loc2];
      p->chd[p->keynum]=rb->chd[0];
      if(rb->chd[0])
       rb->chd[0]->pr=p;
      pr->chd[loc2]=0;              //make null
      for(j=p->keynum+1,i=1;j<=p->keynum+rb->keynum;j++,i++)
      {
       p->data[j]=rb->data[i];
       if(rb->chd[i])
        rb->chd[i]->pr=p;
       p->chd[j]=rb->chd[i];
      }
      p->keynum+=rb->keynum; 
      delnode(pr,pr->data[loc2]);
     }


     
     //the node see

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

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

    };

    void main()
    {
     int key,key1=0;
     key=0;
     btree bt;
     cout<<endl;
     bt.print();
                                               
     for(;;)
     {
     cout<<"enter the node with you want to delete/ncin>:";
     cin>>key>>key1;
    //for(int i=key;i<=key1;i++)
       for(int i=key;i>key1;i--)
     {
        if(i==8)
         bt.call();
    // bt.del(key);
      bt.del(i);
    //  cout<<"the btree delete:"<<key<<endl;
      cout<<"the btree delete:"<<i<<endl;
      bt.print(); 
     }
     }
    }

  • 相关阅读:
    unittest详解 跳过用例的执行(skip)
    python 3 HTMLTestRunner.py文件
    jmeter 如何获取一小时之前的时间戳
    python]用eval强制将字符串转换为字典变量时候出错:NameError: name 'null' is not defined[python]用eval函数 字符串转dict
    Spring Boot 引入自定义yml
    关于爬虫与反爬虫简略方案
    网络回路问题
    MySQL添加新用户、为用户创建数据库、为新用户分配权限
    Spring Boot 项目几种启动方式
    Spring Cloud 之 基础学习资料
  • 原文地址:https://www.cnblogs.com/thubier/p/11944286.html
Copyright © 2011-2022 走看看