zoukankan      html  css  js  c++  java
  • 平衡二叉树.vc6.0

     #include<iostream.h>

    typedef struct nd
    {
     int bf;
     int n;
     struct nd *lch;
     struct nd *rch;
    }btnode,*pbtnode;

    class bltree
    {
    private:
     pbtnode root;
    public:
     bltree()
     {
      root=0;
      int i;
      bool taller;
      cout<<"enter the node:0 is the endl:"<<endl;
      cin>>i;
      while(i)
      {
       Insert(root,i,taller);
       cin>>i;
      }
     }

     void l_rotate(pbtnode &rt)
     {
      pbtnode lc=rt->rch;
      rt->rch=lc->lch;
      lc->lch=rt;
      rt=lc;
     }

     void r_rotate(pbtnode &rt)
     {
      pbtnode rc=rt->lch;
      rt->lch=rc->rch;
      rc->rch=rt;
      rt=rc;
     }

     void leftbanlance(pbtnode &rt)
     {
      pbtnode lc=rt->lch;
      switch(lc->bf)
      {
      case 1:
       rt->bf=lc->bf=0;
       r_rotate(rt);
       break;
      case -1:
       pbtnode rc=lc->rch;
       switch(rc->bf)
       {
       case 1:
        rt->bf=-1;
        lc->bf=0;
        break;
       case -1:
        rt->bf=1;
        lc->bf=0;
        break;
       }
       rc->bf=0;
       l_rotate(lc);
       r_rotate(rt);
       break;
      }
     }

     void rightbanlance(pbtnode &rt)
     {
      pbtnode rc=rt->rch;
      switch(rc->bf)
      {
      case 1:
      {
       pbtnode lc=rc->lch;
       switch(lc->bf)
       {
       case 1:
        rt->bf=0;
        rc->bf=-1;
        break;
       case -1:
        rt->bf=1;
        rc->bf=0;
        break;
       }
       lc->bf=0;
       r_rotate(lc);
       l_rotate(rt);
       break;
      }
      case -1:
       rt->bf=rt->bf=0;
       l_rotate(rt);
       break;
      }
     }

     bool Insert(pbtnode &rt,int i,bool &taller)
     {
      if(!rt)
      {
       rt=new btnode;
       rt->n=i;
       rt->lch=rt->rch=0;
       rt->bf=0;
       taller=true;
      }
      else
      {
       if(rt->n==i)
       {
        taller=false;
        cout<<"it already in here!/n";
        return taller;
       }
       if(i<rt->n)
       {
        if(!Insert(rt->lch,i,taller))
         return 0;
        if(taller)
        {
         switch(rt->bf)
         {
         case 1:
          leftbanlance(rt);
          taller=false;
          break;
         case 0:
          rt->bf=-1;
          taller=true;
          break;
         case -1:
          rt->bf=0;
          taller=false;
          break;
         }
        }    
       }
       else
       {
        if(!(Insert(rt->rch,i,taller)))
         return 0;
        if(taller)
        {
         switch(rt->bf)
         {
         case 1:
          rt->bf=0;
          taller=false;
          break;
         case 0:
          rt->bf=-1;
          taller=true;
          break;
         case -1:
          rightbanlance(rt);
          taller=false;
          break;
         }
        }
       }
      }
      return 1;
     }

     void Inorder(pbtnode rt)
     {
      if(rt)
      {
       Inorder(rt->lch);
       cout<<rt->n<<"  ";
       Inorder(rt->rch);
      }
     }

     void print()
     {
      Inorder(root);
     }
    };

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

  • 相关阅读:
    一幅图解决R语言绘制图例的各种问题
    新时代,建立你的数据分析思维
    新时代,建立你的数据分析思维
    聚类分析基础知识总结及实战解析
    聚类分析基础知识总结及实战解析
    js中 opener和parent的差别
    Latex中參考文献排序
    Android之——清理手机SD卡缓存
    drupal7 使用(hook_preprocess_HOOK)向各个主题模版里面传递变量
    python 正則表達式推断邮箱格式是否正确
  • 原文地址:https://www.cnblogs.com/thubier/p/11944288.html
Copyright © 2011-2022 走看看