第14周 项目1 -(4)平衡二叉树

来源:互联网 发布:淘宝网儿童雨衣 编辑:程序博客网 时间:2024/05/29 11:52

问题描述及代码:

/*     *烟台大学计算机与控制工程学院      *作    者:刘春彤*完成日期:2016年11月29日  *问题描述:认真阅读并验证平衡二叉树相关算法。   (1)由整数序列{43,52,75,24,10,38,67,55,63,60}构造AVL树;   (2)输出用括号法表示的AVL树;   (3)查找关键字55;    (4)分别删除43和55,输出删除后用括号法表示的二叉排序树。     */ 

#include <stdio.h>      #include <malloc.h>      #define MAXL 100    typedef int KeyType;                    //定义关键字类型      typedef char InfoType;      typedef struct node                     //记录类型      {          KeyType key;                        //关键字项          int bf;                             //平衡因子          InfoType data;                      //其他数据域          struct node *lchild,*rchild;        //左右孩子指针      } BSTNode;   typedef struct      {          KeyType key;                //KeyType为关键字的数据类型          InfoType data;              //其他数据      } NodeType;      typedef NodeType SeqList[MAXL];     //顺序表类型    int BinSearch1(SeqList R,int low,int high,KeyType k)      {          int mid;          if (low<=high)      //查找区间存在一个及以上元素          {              mid=(low+high)/2;  //求中间位置              if (R[mid].key==k) //查找成功返回其逻辑序号mid+1                  return mid+1;              if (R[mid].key>k)  //在R[low..mid-1]中递归查找                  BinSearch1(R,low,mid-1,k);              else              //在R[mid+1..high]中递归查找                  BinSearch1(R,mid+1,high,k);          }          else              return 0;      }          void LeftProcess(BSTNode *&p,int &taller)      //对以指针p所指结点为根的二叉树作左平衡旋转处理,本算法结束时,指针p指向新的根结点      {          BSTNode *p1,*p2;          if (p->bf==0)           //原本左、右子树等高,现因左子树增高而使树增高          {              p->bf=1;              taller=1;          }          else if (p->bf==-1)     //原本右子树比左子树高,现左、右子树等高          {              p->bf=0;              taller=0;          }          else                    //原本左子树比右子树高,需作左子树的平衡处理          {              p1=p->lchild;       //p指向*p的左子树根结点              if (p1->bf==1)      //新结点插入在*b的左孩子的左子树上,要作LL调整              {                  p->lchild=p1->rchild;                  p1->rchild=p;                  p->bf=p1->bf=0;                  p=p1;              }              else if (p1->bf==-1)    //新结点插入在*b的左孩子的右子树上,要作LR调整              {                  p2=p1->rchild;                  p1->rchild=p2->lchild;                  p2->lchild=p1;                  p->lchild=p2->rchild;                  p2->rchild=p;                  if (p2->bf==0)          //新结点插在*p2处作为叶子结点的情况                      p->bf=p1->bf=0;                  else if (p2->bf==1)     //新结点插在*p2的左子树上的情况                  {                      p1->bf=0;                      p->bf=-1;                  }                  else                    //新结点插在*p2的右子树上的情况                  {                      p1->bf=1;                      p->bf=0;                  }                  p=p2;                  p->bf=0;            //仍将p指向新的根结点,并置其bf值为0              }              taller=0;          }      }      void RightProcess(BSTNode *&p,int &taller)      //对以指针p所指结点为根的二叉树作右平衡旋转处理,本算法结束时,指针p指向新的根结点      {          BSTNode *p1,*p2;          if (p->bf==0)           //原本左、右子树等高,现因右子树增高而使树增高          {              p->bf=-1;              taller=1;          }          else if (p->bf==1)      //原本左子树比右子树高,现左、右子树等高          {              p->bf=0;              taller=0;          }          else                    //原本右子树比左子树高,需作右子树的平衡处理          {              p1=p->rchild;       //p指向*p的右子树根结点              if (p1->bf==-1)     //新结点插入在*b的右孩子的右子树上,要作RR调整              {                  p->rchild=p1->lchild;                  p1->lchild=p;                  p->bf=p1->bf=0;                  p=p1;              }              else if (p1->bf==1) //新结点插入在*p的右孩子的左子树上,要作RL调整              {                  p2=p1->lchild;                  p1->lchild=p2->rchild;                  p2->rchild=p1;                  p->rchild=p2->lchild;                  p2->lchild=p;                  if (p2->bf==0)          //新结点插在*p2处作为叶子结点的情况                      p->bf=p1->bf=0;                  else if (p2->bf==-1)    //新结点插在*p2的右子树上的情况                  {                      p1->bf=0;                      p->bf=1;                  }                  else                    //新结点插在*p2的左子树上的情况                  {                      p1->bf=-1;                      p->bf=0;                  }                  p=p2;                  p->bf=0;            //仍将p指向新的根结点,并置其bf值为0              }              taller=0;          }      }      int InsertAVL(BSTNode *&b,KeyType e,int &taller)      /*若在平衡的二叉排序树b中不存在和e有相同关键字的结点,则插入一个     数据元素为e的新结点,并返回1,否则返回0。若因插入而使二叉排序树     失去平衡,则作平衡旋转处理,布尔变量taller反映b长高与否*/      {          if(b==NULL)         //原为空树,插入新结点,树“长高”,置taller为1          {              b=(BSTNode *)malloc(sizeof(BSTNode));              b->key=e;              b->lchild=b->rchild=NULL;              b->bf=0;              taller=1;          }          else          {              if (e==b->key)              //树中已存在和e有相同关键字的结点则不再插入              {                  taller=0;                  return 0;              }              if (e<b->key)               //应继续在*b的左子树中进行搜索              {                  if ((InsertAVL(b->lchild,e,taller))==0) //未插入                      return 0;                  if (taller==1)          //已插入到*b的左子树中且左子树“长高”                      LeftProcess(b,taller);              }              else                        //应继续在*b的右子树中进行搜索              {                  if ((InsertAVL(b->rchild,e,taller))==0) //未插入                      return 0;                  if (taller==1)          //已插入到b的右子树且右子树“长高”                      RightProcess(b,taller);              }          }          return 1;      }      void DispBSTree(BSTNode *b) //以括号表示法输出AVL      {          if (b!=NULL)          {              printf("%d",b->key);              if (b->lchild!=NULL || b->rchild!=NULL)              {                  printf("(");                  DispBSTree(b->lchild);                  if (b->rchild!=NULL) printf(",");                  DispBSTree(b->rchild);                  printf(")");              }          }      }      void LeftProcess1(BSTNode *&p,int &taller)  //在删除结点时进行左处理      {          BSTNode *p1,*p2;          if (p->bf==1)          {              p->bf=0;              taller=1;          }          else if (p->bf==0)          {              p->bf=-1;              taller=0;          }          else        //p->bf=-1          {              p1=p->rchild;              if (p1->bf==0)          //需作RR调整              {                  p->rchild=p1->lchild;                  p1->lchild=p;                  p1->bf=1;                  p->bf=-1;                  p=p1;                  taller=0;              }              else if (p1->bf==-1)    //需作RR调整              {                  p->rchild=p1->lchild;                  p1->lchild=p;                  p->bf=p1->bf=0;                  p=p1;                  taller=1;              }              else                    //需作RL调整              {                  p2=p1->lchild;                  p1->lchild=p2->rchild;                  p2->rchild=p1;                  p->rchild=p2->lchild;                  p2->lchild=p;                  if (p2->bf==0)                  {                      p->bf=0;                      p1->bf=0;                  }                  else if (p2->bf==-1)                  {                      p->bf=1;                      p1->bf=0;                  }                  else                  {                      p->bf=0;                      p1->bf=-1;                  }                  p2->bf=0;                  p=p2;                  taller=1;              }          }      }      void RightProcess1(BSTNode *&p,int &taller) //在删除结点时进行右处理      {          BSTNode *p1,*p2;          if (p->bf==-1)          {              p->bf=0;              taller=-1;          }          else if (p->bf==0)          {              p->bf=1;              taller=0;          }          else        //p->bf=1          {              p1=p->lchild;              if (p1->bf==0)          //需作LL调整              {                  p->lchild=p1->rchild;                  p1->rchild=p;                  p1->bf=-1;                  p->bf=1;                  p=p1;                  taller=0;              }              else if (p1->bf==1)     //需作LL调整              {                  p->lchild=p1->rchild;                  p1->rchild=p;                  p->bf=p1->bf=0;                  p=p1;                  taller=1;              }              else                    //需作LR调整              {                  p2=p1->rchild;                  p1->rchild=p2->lchild;                  p2->lchild=p1;                  p->lchild=p2->rchild;                  p2->rchild=p;                  if (p2->bf==0)                  {                      p->bf=0;                      p1->bf=0;                  }                  else if (p2->bf==1)                  {                      p->bf=-1;                      p1->bf=0;                  }                  else                  {                      p->bf=0;                      p1->bf=1;                  }                  p2->bf=0;                  p=p2;                  taller=1;              }          }      }      void Delete2(BSTNode *q,BSTNode *&r,int &taller)      //由DeleteAVL()调用,用于处理被删结点左右子树均不空的情况      {          if (r->rchild==NULL)          {              q->key=r->key;              q=r;              r=r->lchild;              free(q);              taller=1;          }          else          {              Delete2(q,r->rchild,taller);              if (taller==1)                  RightProcess1(r,taller);          }      }      int DeleteAVL(BSTNode *&p,KeyType x,int &taller) //在AVL树p中删除关键字为x的结点      {          int k;          BSTNode *q;          if (p==NULL)              return 0;          else if (x<p->key)          {              k=DeleteAVL(p->lchild,x,taller);              if (taller==1)                  LeftProcess1(p,taller);              return k;          }          else if (x>p->key)          {              k=DeleteAVL(p->rchild,x,taller);              if (taller==1)                  RightProcess1(p,taller);              return k;          }          else            //找到了关键字为x的结点,由p指向它          {              q=p;              if (p->rchild==NULL)        //被删结点右子树为空              {                  p=p->lchild;                  free(q);                  taller=1;              }              else if (p->lchild==NULL)   //被删结点左子树为空              {                  p=p->rchild;                  free(q);                  taller=1;              }              else                        //被删结点左右子树均不空              {                  Delete2(q,q->lchild,taller);                  if (taller==1)                      LeftProcess1(q,taller);                  p=q;              }              return 1;          }      }      int main()      {          BSTNode *b=NULL;          int i,j,k;          KeyType a[]= {43,52,75,24,10,38,67,55,63,60},n=10,x=55;          printf(" 创建一棵AVL树:\n");          for(i=0; i<n; i++)          {              printf("   第%d步,插入%d元素:",i+1,a[i]);              InsertAVL(b,a[i],j);              DispBSTree(b);              printf("\n");          }         printf("   AVL:");          DispBSTree(b);          printf("\n");   <span style="white-space:pre">  </span>        int result;          SeqList R;     <span style="white-space:pre">  </span>for (i=0; i<n; i++)              R[i].key=a[i];          result = BinSearch1(R,0,n-1,x);          if(result>0)              printf("序列中第 %d 个是 %d\n",result, x);          else              printf("木有找到!\n");          printf(" 删除结点:\n");          k=43;        printf("   删除结点%d:",k);          DeleteAVL(b,k,j);          printf("   AVL:");          DispBSTree(b);          printf("\n");          k=55;          printf("   删除结点%d:",k);          DeleteAVL(b,k,j);          printf("   AVL:");          DispBSTree(b);          printf("\n\n");          return 0;      }    

 运行结果:

知识点总结:
平衡二叉树的基本运算

0 0
原创粉丝点击