zoukankan      html  css  js  c++  java
  • 平衡二叉树(AVL)图解与实现

    摘自:https://blog.csdn.net/u014634338/article/details/42465089

    平衡二叉树(Balanced BinaryTree)又被称为AVL树。它具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

    平衡二叉树一般是一个有序树,它具有二叉树的所有性质,其遍历操作和二叉树的遍历操作相同。但是由于其对二叉树施加了额外限制,因而其添加、删除操作都必须保证平衡二叉树的因子被保持。

    平衡二叉树中引入了一个概念:平衡二叉树节点的平衡因子,它指的是该节点的两个子树,即左子树和右子树的高度差,即用左子树的高度减去右子树的高度,如果该节点的某个子树不存在,则该子树的高度为0,如果高度差的绝对值超过1就要根据情况进行调整。

    为了更好的明白下面的图解和代码,这里我先给出平衡二叉树结构定义:

    1.  
      typedef struct AVLNode *Tree;
    2.  
      typedef int ElementType;
    3.  
      struct AVLNode
    4.  
      {
    5.  
      int depth; //深度,这里计算每个结点的深度,通过深度的比较可得出是否平衡
    6.  
      Tree parent; //该结点的父节点,方便操作
    7.  
      ElementType val; //结点值
    8.  
      Tree lchild;
    9.  
      Tree rchild;
    10.  
      AVLNode(int val=0) //默认构造函数
    11.  
      {
    12.  
      parent=NULL;
    13.  
      depth=0;
    14.  
      lchild=rchild=NULL;
    15.  
      this->val=val;
    16.  
      }
    17.  
      };

    平衡的调整共有四种情况:分别为LL,LR,RR,RL。

    下面我们通过不断插入数据来说明几种不同的旋转方式:

    注意:橘黄色的结点为旋转中心,黑色结点的为离插入结点最近的失衡结点。

    (1)LR型

    最开始插入数据16,3,7后的结构如上图所示,结点16失去了平衡,3为16的左孩子,7为失衡结点的左孩子的右孩子,所以为LR型,接下来通过两次旋转操作复衡,先通过以3为旋转中心,进行左旋转,结果如图所示,然后再以7为旋转中心进行右旋转,旋转后恢复平衡了。

    1.  
      //LR型,先左旋转,再右旋转
    2.  
      //返回:新父节点
    3.  
      Tree LR_rotate(Tree node)
    4.  
      {
    5.  
      RR_rotate(node->lchild);
    6.  
      return LL_rotate(node);
    7.  
      }

    (2)LL型

    在上面恢复平衡后我们再次插入数据11和9,发现又失去平衡了,这次失衡结点是16,11是其左孩子,9为其失衡结点的左孩子的左孩子,所以是LL型,以失衡结点的左孩子为旋转中心进行一次右旋转即可。

    1.  
      //LL型调整函数
    2.  
      //返回:新父节点
    3.  
      Tree LL_rotate(Tree node)
    4.  
      {
    5.  
      //node为离操作结点最近的失衡的结点
    6.  
       
    7.  
      Tree parent=NULL,son;
    8.  
      //获取失衡结点的父节点
    9.  
      parent=node->parent;
    10.  
      //获取失衡结点的左孩子
    11.  
      son=node->lchild;
    12.  
       
    13.  
      //设置son结点右孩子的父指针
    14.  
      if (son->rchild!=NULL)
    15.  
      son->rchild->parent=node;
    16.  
       
    17.  
      //失衡结点的左孩子变更为son的右孩子
    18.  
      node->lchild=son->rchild;
    19.  
       
    20.  
      //更新失衡结点的高度信息
    21.  
      update_depth(node);
    22.  
       
    23.  
      //失衡结点变成son的右孩子
    24.  
      son->rchild=node;
    25.  
       
    26.  
      //设置son的父结点为原失衡结点的父结点
    27.  
      son->parent=parent;
    28.  
       
    29.  
       
    30.  
      //如果失衡结点不是根结点,则开始更新父节点
    31.  
      if (parent!=NULL)
    32.  
      {
    33.  
      //如果父节点的左孩子是失衡结点,指向现在更新后的新孩子son
    34.  
      if (parent->lchild==node)
    35.  
      parent->lchild=son;
    36.  
      else //父节点的右孩子是失衡结点
    37.  
      parent->rchild=son;
    38.  
      }
    39.  
      //设置失衡结点的父亲
    40.  
      node->parent=son;
    41.  
      //更新son结点的高度信息
    42.  
      update_depth(son);
    43.  
      return son;
    44.  
      }

    (3)RR型

    进一步插入数据26后又再次失衡了,失衡结点为7,很明显这是RR型,以失衡结点的右孩子为旋转中心左旋转一次即可。

    代码:

    1.  
      //RR型调整函数
    2.  
      //返回新父节点
    3.  
      Tree RR_rotate(Tree node)
    4.  
      {
    5.  
      //node为离操作结点最近的失衡的结点
    6.  
       
    7.  
      Tree parent=NULL,son;
    8.  
      //获取失衡结点的父节点
    9.  
      parent=node->parent;
    10.  
      //获取失衡结点的右孩子
    11.  
      son=node->rchild;
    12.  
       
    13.  
      //设置son结点左孩子的父指针
    14.  
      if (son->lchild!=NULL)
    15.  
      son->lchild->parent=node;
    16.  
       
    17.  
      //失衡结点的右孩子变更为son的左孩子
    18.  
      node->rchild=son->lchild;
    19.  
       
    20.  
      //更新失衡结点的高度信息
    21.  
      update_depth(node);
    22.  
       
    23.  
      //失衡结点变成son的左孩子
    24.  
      son->lchild=node;
    25.  
       
    26.  
      //设置son的父结点为原失衡结点的父结点
    27.  
      son->parent=parent;
    28.  
       
    29.  
       
    30.  
      //如果失衡结点不是根结点,则开始更新父节点
    31.  
      if (parent!=NULL)
    32.  
      {
    33.  
      //如果父节点的左孩子是失衡结点,指向现在更新后的新孩子son
    34.  
      if (parent->lchild==node)
    35.  
      parent->lchild=son;
    36.  
      else //父节点的右孩子是失衡结点
    37.  
      parent->rchild=son;
    38.  
      }
    39.  
      //设置失衡结点的父亲
    40.  
      node->parent=son;
    41.  
      //更新son结点的高度信息
    42.  
      update_depth(son);
    43.  
      return son;
    44.  
      }

    (4)RL型

    再插入18后又再次失衡了,失衡结点为16,26为其右孩子,18为其右孩子的左孩子,为RL型,以失衡结点的右孩子为旋转中心,进行一次右旋转,然后再次已失衡结点的右孩子为旋转中心进行一次左旋转变恢复了平衡。

    1.  
      //RL型,先右旋转,再左旋转
    2.  
      //返回:新父节点
    3.  
      Tree RL_rotate(Tree node)
    4.  
      {
    5.  
      LL_rotate(node->rchild);
    6.  
      return RR_rotate(node);
    7.  
      }

    这就是4中旋转方式,其实只有两种,RR和LL,RL和LR本质上是一样的。下面我们再次插入数据14,15,完成我们最后数据的插入操作:

    又是一次LR型,按前面操作就可以了。

    一、平衡二叉树的建立

    说完了平衡二叉树的4个基本调整,基本问题就解决一大半了,接下来我们再来说说其它的操作函数,首先我们得建树对吧,既然要建树,那么就得插入数据呀,来看看我们的插入函数:

    1.  
      //向AVL树中插入val
    2.  
      //参数:根,插入数据value
    3.  
      //返回:新根结点
    4.  
      Tree Insert(Tree &root,ElementType val)
    5.  
      {
    6.  
      Tree temp=NULL;
    7.  
      Tree node=new AVLNode(val);
    8.  
       
    9.  
      //插入结点
    10.  
      temp=insert_val(root,node,NULL); //调用真正的插入函数
    11.  
       
    12.  
      if (temp)
    13.  
      {
    14.  
      update_depth(temp);
    15.  
      root=AVLTree(root,temp); //检查树是否该调整
    16.  
      }
    17.  
      else //无需插入,释放结点
    18.  
      delete temp;
    19.  
      return root;
    20.  
      }

    这里我们看到,我们在插入数据里面还有一个真正的插入函数,以及高度更新操作,还有我们AVL树调整操作,而我们刚刚的插入函数封装了封装了这两个函数,这样方便我们的调用,我们可以先不管其细节如果,先理解上面的逻辑结构,我们在函数里面创建一个结点,然后插入这个数据,当然不一定成功呢,如果这个AVL树中有了这个数据,那么插入就会失败,temp将等于空,所以我们对temp可以进行判断,再进行操作,当插入成功后,我们更新插入结点的高度信息,其实这个结点肯定是叶子结点,不用更新也可以,因为我们在构造函数里面已经初始化了。高度更新后,我们便开始看是否需要调整树结构,将调整后的新根结点返回来。这就是这段代码的逻辑结构,很简单吧,接下来我们在分析其分支结构。

    1.  
      //更新当前深度
    2.  
      void update_depth(Tree node)
    3.  
      {
    4.  
      if (node==NULL)
    5.  
      return;
    6.  
      else
    7.  
      {
    8.  
      int depth_Lchild=get_balance(node->lchild); //左孩子深度
    9.  
      int depth_Rchild=get_balance(node->rchild); //右孩子深度
    10.  
      node->depth=max(depth_Lchild,depth_Rchild)+1;
    11.  
      }
    12.  
      }


    这便是我们的高度更新函数,这里面又有一个新函数,就是获取左右子树的高度,然后再更新当前结点的高度信息。很简单吧。接下来看看我们的获取高度的函数.

    1.  
      //获取当前结点的深度
    2.  
      int get_balance(Tree node)
    3.  
      {
    4.  
      if (node==NULL)
    5.  
      return 0;
    6.  
      return node->depth;
    7.  
      }

    可以看到这个函数非常的简单,就是直接返回当前结点的高度而已,单独写个函数,只是为了便于梳理逻辑结构,便于调用,也许大家这里就有问题了,我们每次都是获取当前结点里面的高度,但是怎么确保我们里面每个结点的高度信息都是准确的呢,为了我们每个结点高度信息的准确,所以我们每插入一个结点就得一层一层的更新结点的高度信息,因为建树过程是从无到有,我们最开始就慢慢维护好每个结点的高度,一旦变动我们就更新一下,不管是否需要,这样我们就可以保证准确性了,其实这个问题我们可以放在后面再来理解的。

    OK,到了这里我们还有一个个非常重要的函数没有讲解,就是AVLTree,AVL树调整函数,里面了前面的逻辑结构我们现在就往下看:

    1.  
      //AVL树调整函数
    2.  
      //参数:根结点,插入结点
    3.  
      //返回:调整后的根结点
    4.  
      Tree AVLTree(Tree &root,Tree node)
    5.  
      {
    6.  
      int balance=0; //平衡因子
    7.  
      while (node!=NULL) //检查其祖先是否需要调整,更新
    8.  
      {
    9.  
      update_depth(node); //更新当前结点的高度信息
    10.  
      balance=is_balance(node); //获取当前结点的平衡因子情况
    11.  
      if (balance>1 || balance<-1) //平衡因子超标
    12.  
      {
    13.  
      if (balance>1) //左子树高
    14.  
      {
    15.  
      if (is_balance(node->lchild)>0) //LL型
    16.  
      node=LL_rotate(node);
    17.  
      else //LR型
    18.  
      node=LR_rotate(node);
    19.  
      }
    20.  
      else //右子树高
    21.  
      {
    22.  
      if (is_balance(node->rchild)<0) //RR型
    23.  
      node=RR_rotate(node);
    24.  
      else //RL型
    25.  
      node=RL_rotate(node);
    26.  
      }
    27.  
      if (node->parent==NULL) //到达根结点
    28.  
      {
    29.  
      root=node; //设置新的根结点
    30.  
      break; //退出
    31.  
      }
    32.  
      }
    33.  
      node=node->parent; //依次找到其父节点
    34.  
       
    35.  
      }
    36.  
      return root; //返回新根
    37.  
      }

    重要看到其真面目了,很惊讶的是,怎么这么短呢,其实我们把各个功能分开了,这样方便编写,不然很混乱,这里我们终于看到了我们的平衡因子,这里的平衡因子我们由树的高度得出,判断当前这个树是否平衡,那么我们只要得出这个树的左右子树的高度变可以判断这颗树是否平衡了,如果不平衡,那么我们就判断是4中类型的那种,然后进行相应操作即可了,代码里面的注释我觉得我还是写得很详细了,只要认真看,结合图解肯定是看得懂的。最后我们看到这里有个while循环,其实这个循环就是一层一层检查祖先是否平衡,以及更新他们的高度信息,这样我们就能维护好一颗树啦,注意根结点的父节点是为空的,这样我们最后便可以顺利的退出来啦。

    这里的4种操作我在前面就给出来了,这里就不在重复给出来了,可以看看前面,注释很详细的。

    到这里平衡二叉树的建树过程就完成了,只需平衡二叉树的查找函数很简单,这里就不在给出来,其实自己也没有写,当然这个可以完全自己写,平衡二叉树也是一颗BST树,BST的查找函数完全适用。

    二、平衡二叉树的删除

    前面我们说了平衡二叉树的建树过程,只要明白怎么旋转,以及怎么进行平衡的判断其实很简单的,不同的判断方法可能导致代码差别很大,所以选择好的判断方式还是很重要的。AVL树的删除操作其实比较简单的,因为这里我们以及有了调整函数,这样我们再删除的过程中如果一旦判断不平衡了,那么我们就通过平衡调整函数进行调整即可。平衡二叉树的删除方式和BST的删除方式是一样的,都是化繁为简,如果看到这里不知道BST怎么删除元素的可以参考这篇博客:BST的建立于删除

    OK,我们这里假设大家已经会了BST的删除操作,我们直接进行AVL树的操作,先看一下删除函数:

    1.  
      //找到删除的结点,执行删除操作,并根据情况调整AVL树
    2.  
      //参数:根,需要删除的val
    3.  
      //返回:找到删除结点的情况则返回新根,否则返回NULL
    4.  
      Tree remove(Tree &root,ElementType val)
    5.  
      {
    6.  
      static Tree *temp=NULL;
    7.  
      if (root==NULL)
    8.  
      {
    9.  
      temp=NULL;
    10.  
      return NULL;
    11.  
      }
    12.  
      else if(root->val<val) //在右子树查找
    13.  
      remove(root->rchild, val);
    14.  
      else if(root->val>val) //在左子树查找
    15.  
      remove(root->lchild, val);
    16.  
      else //找到了,标记一下
    17.  
      temp=&root;
    18.  
       
    19.  
      if (temp)
    20.  
      {
    21.  
      if (!root->parent) //如果已经返回到最后一次(也就是root是真正的树根)
    22.  
      {
    23.  
      Tree tmp=NULL;
    24.  
      tmp=remove_val(root,*temp); //执行删除操作
    25.  
      return AVLTree(root,tmp); //更新AVL树
    26.  
      }
    27.  
      return *temp;
    28.  
      }
    29.  
      return NULL;
    30.  
      }

    OK,认真看还是很简单的,这里一直查找这个需要删除的结点,然后赋值给这个temp,这样我们再函数即将退出的时候,进行相应的判断操作,如果找了,那么我们调用一个删除函数,进行删除,删除后调整一下AVL树,注意这里的temp是个二级结构体指针,会引用需要删除的结点。

    1.  
      //删除操作
    2.  
      //参数:根,需要删除的结点
    3.  
      //返回值: 返回删除结点的父节点
    4.  
      Tree remove_val(Tree &root,Tree &node)
    5.  
      {
    6.  
      Tree parent=node->parent;
    7.  
      Tree temp=NULL;
    8.  
      //只有左孩子
    9.  
      if (node->rchild==NULL && node->lchild!=NULL)
    10.  
      {
    11.  
      temp=node;
    12.  
      node=node->lchild; //指向左孩子
    13.  
      node->parent=temp->parent;
    14.  
      delete temp; //释放结点
    15.  
      update_depth(node); //更新当前结点信息
    16.  
      }
    17.  
      else if(node->lchild==NULL && node->rchild!=NULL) //只有右孩子
    18.  
      {
    19.  
      temp=node;
    20.  
      node=node->rchild; //指向右结点
    21.  
      node->parent=temp->parent;
    22.  
      delete temp; //释放结点
    23.  
      update_depth(node); //更新当前结点信息
    24.  
      }
    25.  
      else if(node->rchild==NULL && node->lchild==NULL) //叶子结点
    26.  
      {
    27.  
      parent=node->parent; //找到其父节点
    28.  
      if (parent) //如果父节点存在
    29.  
      {
    30.  
      /*
    31.  
      if (parent->lchild==node)//当前结点是父节点的左孩子
    32.  
      {
    33.  
      parent->lchild=0; //删掉左孩子
    34.  
      delete node; //释放空间
    35.  
      }
    36.  
      else //当前结点是父节点的右孩子
    37.  
      {
    38.  
      parent->rchild=0;
    39.  
      delete node;
    40.  
      }
    41.  
      */
    42.  
      delete node;
    43.  
      node=NULL;
    44.  
      update_depth(parent); //更新父节点高度信息
    45.  
      }
    46.  
      else //删除的是根
    47.  
      {
    48.  
      delete root;
    49.  
      root=NULL;
    50.  
      }
    51.  
      }
    52.  
      else //既有左孩子也有右孩子,化繁为简
    53.  
      {
    54.  
      Tree *tmp=Find_Min(node->rchild); //找到替代元素,temp为叶子结点
    55.  
      node->val=(*tmp)->val; //更新值
    56.  
      //判断当前叶子结点是左孩子还是右孩子。
    57.  
      parent=(*tmp)->parent;
    58.  
      /*
    59.  
      if (parent->lchild==temp)
    60.  
      {
    61.  
      parent->lchild=0;
    62.  
      delete temp;
    63.  
      }
    64.  
      else
    65.  
      {
    66.  
      parent->rchild=0;
    67.  
      delete temp;
    68.  
      }
    69.  
      */
    70.  
      delete *tmp;
    71.  
      *tmp=NULL;
    72.  
      update_depth(parent);
    73.  
      }
    74.  
      return parent;
    75.  
      }


    这便是我们真正的删除函数,里面分了几种不同的情况,同时对于复杂的情况我们化繁为简,这里大家注意到,我们注释了一段代码,这是在开始的时候,我没有引用删除结点指针,这样需要通过父节点来进行操作,需要判断是父节点的左孩子还是右孩子,然后再进行相应的操作,后来为了统一,把Find_Min函数改为返回其引用结构,这样就可以不用判断了,这样变可以简化很多(这和BST里面是一样的),不过也极容易操作出错,而影响树结构,可以看看几种旋转操作,为了方便理解,没有改成这种结构。

    OK到这里,我们就把二叉平衡树的删除操作说完了,相对来说只要旋转有了,那么删除也很简单了,只是细节处理稍微麻烦点,反正一旦处理了某个结点,那么一定要层层更新。维护好树结构。特别是删除操作。有可能最近祖先没有失衡,但是稍远的祖先确失衡了。至于删除的图解应该不用话出来了吧,后面我给出数据,然后自己可以在纸上画一下,看看是否正确。

    看看main函数:

    1.  
      int main()
    2.  
      {
    3.  
      Tree root=NULL;
    4.  
      root = Insert(root, 16);
    5.  
      root = Insert(root, 3);
    6.  
       
    7.  
      //插入7后LR调整
    8.  
      root = Insert(root, 7);
    9.  
      root = Insert(root, 11);
    10.  
       
    11.  
      //插入9后LL调整
    12.  
      root = Insert(root, 9);
    13.  
       
    14.  
      //插入26后RR调整
    15.  
      root = Insert(root, 26);
    16.  
       
    17.  
      //插入28后RL调整
    18.  
      root = Insert(root, 18);
    19.  
      root = Insert(root, 14);
    20.  
       
    21.  
      //插入15后LR调整
    22.  
      root = Insert(root, 15);
    23.  
       
    24.  
      printf("插入: ");
    25.  
      printf("前序:");
    26.  
      PreOrder(root); // 11 7 3 9 18 15 14 16 26
    27.  
      printf(" ");
    28.  
       
    29.  
       
    30.  
      printf("中序:");
    31.  
      InOrder(root); // 3 7 9 11 14 15 16 18 26
    32.  
      printf(" ");
    33.  
       
    34.  
       
    35.  
      printf("删除: ");
    36.  
       
    37.  
      //测试删除叶子结点
    38.  
      // remove(root, 16);
    39.  
       
    40.  
       
    41.  
      //测试删除只有左孩子的结点
    42.  
      // remove(root, 16);
    43.  
      // remove(root, 15);
    44.  
       
    45.  
       
    46.  
      //测试删除只有右孩子的结点
    47.  
      // remove(root, 14);
    48.  
      // remove(root, 15);
    49.  
       
    50.  
      //测试删除有左右孩子的结点
    51.  
      // remove(root, 18);
    52.  
       
    53.  
      //删除26后进行LR型调整
    54.  
      remove(root, 26);
    55.  
       
    56.  
       
    57.  
      //删除18后进行RR型
    58.  
      remove(root, 18);
    59.  
       
    60.  
       
    61.  
      remove(root, 3);
    62.  
      remove(root, 9);
    63.  
       
    64.  
      //删除7过进行RL调整
    65.  
      remove(root, 7);
    66.  
       
    67.  
       
    68.  
      //删除11后进行LL调整
    69.  
      remove(root, 11);
    70.  
       
    71.  
      //把结点删除完
    72.  
      // remove(root, 15);
    73.  
      // remove(root, 14);
    74.  
      // remove(root, 16);
    75.  
       
    76.  
      printf("前序:");
    77.  
      PreOrder(root);
    78.  
      printf(" ");
    79.  
       
    80.  
       
    81.  
      printf("中序:");
    82.  
      InOrder(root);
    83.  
      printf(" ");
    84.  
       
    85.  
       
    86.  
      return 0;
    87.  
      }


    前面的插入就不说了,只要说说删除,因为测试的时候,数据很重要,开始就是测试的不够好,导致错误代码混过去了,后来改正了,不过可能还是有bug,忘各位不吝指出。

    1.  
      //测试删除叶子结点
    2.  
      // remove(root, 16);
    3.  
       
    4.  
       
    5.  
      //测试删除只有左孩子的结点
    6.  
      // remove(root, 16);
    7.  
      // remove(root, 15);
    8.  
       
    9.  
       
    10.  
      //测试删除只有右孩子的结点
    11.  
      // remove(root, 14);
    12.  
      // remove(root, 15);
    13.  
       
    14.  
      //测试删除有左右孩子的结点
    15.  
      // remove(root, 18);


    这几个测试点,是单独测试的,也就是它们之间是独立的,分别测试4种不同的情况,比如测试第二种的是否,请注释第一组和其它组,不过乱测试还是可以的,后面的旋转测试,是一步接着一步的,不是独立的。

    OK,终于说完了,下面贴出全部代码,希望和大家一起学习,如果有问题,希望各位可以指出,谢谢啦。

    1.  
      #include <stdio.h>
    2.  
      #include <stdlib.h>
    3.  
      #include <iostream>
    4.  
      using namespace std;
    5.  
      typedef struct AVLNode *Tree;
    6.  
      typedef int ElementType;
    7.  
      struct AVLNode
    8.  
      {
    9.  
      int depth; //深度,这里计算每个结点的深度,通过深度的比较可得出是否平衡
    10.  
      Tree parent; //该结点的父节点,方便操作
    11.  
      ElementType val; //结点值
    12.  
      Tree lchild;
    13.  
      Tree rchild;
    14.  
      AVLNode(int val=0) //默认构造函数
    15.  
      {
    16.  
      parent=NULL;
    17.  
      depth=0;
    18.  
      lchild=rchild=NULL;
    19.  
      this->val=val;
    20.  
      }
    21.  
      };
    22.  
      Tree insert_val(Tree&,Tree,Tree);
    23.  
      Tree remove(Tree&,ElementType);
    24.  
      Tree remove_val(Tree &,Tree &);
    25.  
      void update_depth(Tree);
    26.  
      Tree AVLTree(Tree&,Tree);
    27.  
      Tree LL_rotate(Tree);
    28.  
      Tree RL_rotate(Tree);
    29.  
      Tree RR_rotate(Tree);
    30.  
      Tree LR_rotate(Tree);
    31.  
      int get_balance(Tree);
    32.  
      int is_balance(Tree);
    33.  
      Tree *Find_Min(Tree&);
    34.  
       
    35.  
      //向AVL树中插入val
    36.  
      //参数:根,插入数据value
    37.  
      //返回:新根结点
    38.  
      Tree Insert(Tree &root,ElementType val)
    39.  
      {
    40.  
      Tree temp=NULL;
    41.  
      Tree node=new AVLNode(val);
    42.  
       
    43.  
      //插入结点
    44.  
      temp=insert_val(root,node,NULL); //调用真正的插入函数
    45.  
       
    46.  
      if (temp)
    47.  
      {
    48.  
      update_depth(temp);
    49.  
      root=AVLTree(root,temp); //检查树是否该调整
    50.  
      }
    51.  
      else //无需插入,释放结点
    52.  
      delete temp;
    53.  
      return root;
    54.  
      }
    55.  
      //插入函数
    56.  
      //参数:根节点,待插结点,待插结点的父节点
    57.  
      //返回:插入结点
    58.  
      Tree insert_val(Tree &root,Tree node,Tree parent)
    59.  
      {
    60.  
      if (root==NULL)
    61.  
      {
    62.  
      root=node;
    63.  
      node->parent=parent; //设置当前结点的父结点
    64.  
      return root; //返回插入结点
    65.  
      }
    66.  
      if (node->val<root->val) //插左子树
    67.  
      return insert_val(root->lchild, node,root);
    68.  
      else if(node->val>root->val) //插右子树
    69.  
      return insert_val(root->rchild, node,root);
    70.  
      else //已存在该结点,停止插入操作,返回NULL
    71.  
      return NULL;
    72.  
      }
    73.  
       
    74.  
      //AVL树调整函数
    75.  
      //参数:根结点,插入结点
    76.  
      //返回:调整后的根结点
    77.  
      Tree AVLTree(Tree &root,Tree node)
    78.  
      {
    79.  
      int balance=0; //平衡因子
    80.  
      while (node!=NULL) //检查其祖先是否需要调整,更新
    81.  
      {
    82.  
      update_depth(node); //更新当前结点的高度信息
    83.  
      balance=is_balance(node); //获取当前结点的平衡因子情况
    84.  
      if (balance>1 || balance<-1) //平衡因子超标
    85.  
      {
    86.  
      if (balance>1) //左子树高
    87.  
      {
    88.  
      if (is_balance(node->lchild)>0) //LL型
    89.  
      node=LL_rotate(node);
    90.  
      else //LR型
    91.  
      node=LR_rotate(node);
    92.  
      }
    93.  
      else //右子树高
    94.  
      {
    95.  
      if (is_balance(node->rchild)<0) //RR型
    96.  
      node=RR_rotate(node);
    97.  
      else //RL型
    98.  
      node=RL_rotate(node);
    99.  
      }
    100.  
      if (node->parent==NULL) //到达根结点
    101.  
      {
    102.  
      root=node; //设置新的根结点
    103.  
      break; //退出
    104.  
      }
    105.  
      }
    106.  
      node=node->parent; //依次找到其父节点
    107.  
       
    108.  
      }
    109.  
      return root; //返回新根
    110.  
      }
    111.  
       
    112.  
       
    113.  
       
    114.  
      //查找最小结点
    115.  
      Tree *Find_Min(Tree &root)
    116.  
      {
    117.  
      if (root->lchild)
    118.  
      {
    119.  
      return Find_Min(root->lchild);
    120.  
      }
    121.  
      return &root;
    122.  
      }
    123.  
       
    124.  
       
    125.  
      //删除操作
    126.  
      //参数:根,需要删除的结点
    127.  
      //返回值: 返回删除结点的父节点
    128.  
      Tree remove_val(Tree &root,Tree &node)
    129.  
      {
    130.  
      Tree parent=node->parent;
    131.  
      Tree temp=NULL;
    132.  
      //只有左孩子
    133.  
      if (node->rchild==NULL && node->lchild!=NULL)
    134.  
      {
    135.  
      temp=node;
    136.  
      node=node->lchild; //指向左孩子
    137.  
      node->parent=temp->parent;
    138.  
      delete temp; //释放结点
    139.  
      update_depth(node); //更新当前结点信息
    140.  
      }
    141.  
      else if(node->lchild==NULL && node->rchild!=NULL) //只有右孩子
    142.  
      {
    143.  
      temp=node;
    144.  
      node=node->rchild; //指向右结点
    145.  
      node->parent=temp->parent;
    146.  
      delete temp; //释放结点
    147.  
      update_depth(node); //更新当前结点信息
    148.  
      }
    149.  
      else if(node->rchild==NULL && node->lchild==NULL) //叶子结点
    150.  
      {
    151.  
      parent=node->parent; //找到其父节点
    152.  
      if (parent) //如果父节点存在
    153.  
      {
    154.  
      /*
    155.  
      if (parent->lchild==node)//当前结点是父节点的左孩子
    156.  
      {
    157.  
      parent->lchild=0; //删掉左孩子
    158.  
      delete node; //释放空间
    159.  
      }
    160.  
      else //当前结点是父节点的右孩子
    161.  
      {
    162.  
      parent->rchild=0;
    163.  
      delete node;
    164.  
      }
    165.  
      */
    166.  
      delete node;
    167.  
      node=NULL;
    168.  
      update_depth(parent); //更新父节点高度信息
    169.  
      }
    170.  
      else //删除的是根
    171.  
      {
    172.  
      delete root;
    173.  
      root=NULL;
    174.  
      }
    175.  
      }
    176.  
      else //既有左孩子也有右孩子,化繁为简
    177.  
      {
    178.  
      Tree *tmp=Find_Min(node->rchild); //找到替代元素,temp为叶子结点
    179.  
      node->val=(*tmp)->val; //更新值
    180.  
      //判断当前叶子结点是左孩子还是右孩子。
    181.  
      parent=(*tmp)->parent;
    182.  
      /*
    183.  
      if (parent->lchild==temp)
    184.  
      {
    185.  
      parent->lchild=0;
    186.  
      delete temp;
    187.  
      }
    188.  
      else
    189.  
      {
    190.  
      parent->rchild=0;
    191.  
      delete temp;
    192.  
      }
    193.  
      */
    194.  
      delete *tmp;
    195.  
      *tmp=NULL;
    196.  
      update_depth(parent);
    197.  
      }
    198.  
      return parent;
    199.  
      }
    200.  
       
    201.  
      //找到删除的结点,执行删除操作,并根据情况调整AVL树
    202.  
      //参数:根,需要删除的val
    203.  
      //返回:找到删除结点的情况则返回新根,否则返回NULL
    204.  
      Tree remove(Tree &root,ElementType val)
    205.  
      {
    206.  
      static Tree *temp=NULL;
    207.  
      if (root==NULL)
    208.  
      {
    209.  
      temp=NULL;
    210.  
      return NULL;
    211.  
      }
    212.  
      else if(root->val<val) //在右子树查找
    213.  
      remove(root->rchild, val);
    214.  
      else if(root->val>val) //在左子树查找
    215.  
      remove(root->lchild, val);
    216.  
      else //找到了,标记一下
    217.  
      temp=&root;
    218.  
       
    219.  
      if (temp)
    220.  
      {
    221.  
      if (!root->parent) //如果已经返回到最后一次(也就是root是真正的树根)
    222.  
      {
    223.  
      Tree tmp=NULL;
    224.  
      tmp=remove_val(root,*temp); //执行删除操作
    225.  
      return AVLTree(root,tmp); //更新AVL树
    226.  
      }
    227.  
      return *temp;
    228.  
      }
    229.  
      return NULL;
    230.  
      }
    231.  
       
    232.  
      //获取当前结点的深度
    233.  
      int get_balance(Tree node)
    234.  
      {
    235.  
      if (node==NULL)
    236.  
      return 0;
    237.  
      return node->depth;
    238.  
      }
    239.  
      //返回当前平衡因子
    240.  
      int is_balance(Tree node)
    241.  
      {
    242.  
      if (node==NULL)
    243.  
      return 0;
    244.  
      else
    245.  
      return get_balance(node->lchild)-get_balance(node->rchild);
    246.  
      }
    247.  
       
    248.  
      //RR型调整函数
    249.  
      //返回新父节点
    250.  
      Tree RR_rotate(Tree node)
    251.  
      {
    252.  
      //node为离操作结点最近的失衡的结点
    253.  
       
    254.  
      Tree parent=NULL,son;
    255.  
      //获取失衡结点的父节点
    256.  
      parent=node->parent;
    257.  
      //获取失衡结点的右孩子
    258.  
      son=node->rchild;
    259.  
       
    260.  
      //设置son结点左孩子的父指针
    261.  
      if (son->lchild!=NULL)
    262.  
      son->lchild->parent=node;
    263.  
       
    264.  
      //失衡结点的右孩子变更为son的左孩子
    265.  
      node->rchild=son->lchild;
    266.  
       
    267.  
      //更新失衡结点的高度信息
    268.  
      update_depth(node);
    269.  
       
    270.  
      //失衡结点变成son的左孩子
    271.  
      son->lchild=node;
    272.  
       
    273.  
      //设置son的父结点为原失衡结点的父结点
    274.  
      son->parent=parent;
    275.  
       
    276.  
       
    277.  
      //如果失衡结点不是根结点,则开始更新父节点
    278.  
      if (parent!=NULL)
    279.  
      {
    280.  
      //如果父节点的左孩子是失衡结点,指向现在更新后的新孩子son
    281.  
      if (parent->lchild==node)
    282.  
      parent->lchild=son;
    283.  
      else //父节点的右孩子是失衡结点
    284.  
      parent->rchild=son;
    285.  
      }
    286.  
      //设置失衡结点的父亲
    287.  
      node->parent=son;
    288.  
      //更新son结点的高度信息
    289.  
      update_depth(son);
    290.  
      return son;
    291.  
      }
    292.  
      //LL型调整函数
    293.  
      //返回:新父节点
    294.  
      Tree LL_rotate(Tree node)
    295.  
      {
    296.  
      //node为离操作结点最近的失衡的结点
    297.  
       
    298.  
      Tree parent=NULL,son;
    299.  
      //获取失衡结点的父节点
    300.  
      parent=node->parent;
    301.  
      //获取失衡结点的左孩子
    302.  
      son=node->lchild;
    303.  
       
    304.  
      //设置son结点右孩子的父指针
    305.  
      if (son->rchild!=NULL)
    306.  
      son->rchild->parent=node;
    307.  
       
    308.  
      //失衡结点的左孩子变更为son的右孩子
    309.  
      node->lchild=son->rchild;
    310.  
       
    311.  
      //更新失衡结点的高度信息
    312.  
      update_depth(node);
    313.  
       
    314.  
      //失衡结点变成son的右孩子
    315.  
      son->rchild=node;
    316.  
       
    317.  
      //设置son的父结点为原失衡结点的父结点
    318.  
      son->parent=parent;
    319.  
       
    320.  
       
    321.  
      //如果失衡结点不是根结点,则开始更新父节点
    322.  
      if (parent!=NULL)
    323.  
      {
    324.  
      //如果父节点的左孩子是失衡结点,指向现在更新后的新孩子son
    325.  
      if (parent->lchild==node)
    326.  
      parent->lchild=son;
    327.  
      else //父节点的右孩子是失衡结点
    328.  
      parent->rchild=son;
    329.  
      }
    330.  
      //设置失衡结点的父亲
    331.  
      node->parent=son;
    332.  
      //更新son结点的高度信息
    333.  
      update_depth(son);
    334.  
      return son;
    335.  
      }
    336.  
       
    337.  
      //LR型,先左旋转,再右旋转
    338.  
      //返回:新父节点
    339.  
      Tree LR_rotate(Tree node)
    340.  
      {
    341.  
      RR_rotate(node->lchild);
    342.  
      return LL_rotate(node);
    343.  
      }
    344.  
       
    345.  
      //RL型,先右旋转,再左旋转
    346.  
      //返回:新父节点
    347.  
      Tree RL_rotate(Tree node)
    348.  
      {
    349.  
      LL_rotate(node->rchild);
    350.  
      return RR_rotate(node);
    351.  
      }
    352.  
      //更新当前深度
    353.  
      void update_depth(Tree node)
    354.  
      {
    355.  
      if (node==NULL)
    356.  
      return;
    357.  
      else
    358.  
      {
    359.  
      int depth_Lchild=get_balance(node->lchild); //左孩子深度
    360.  
      int depth_Rchild=get_balance(node->rchild); //右孩子深度
    361.  
      node->depth=max(depth_Lchild,depth_Rchild)+1;
    362.  
      }
    363.  
      }
    364.  
      //前序
    365.  
      void PreOrder(Tree root)
    366.  
      {
    367.  
      if (root==NULL)
    368.  
      return;
    369.  
      printf("%d ",root->val);
    370.  
      PreOrder(root->lchild);
    371.  
      PreOrder(root->rchild);
    372.  
      }
    373.  
      //中序
    374.  
      void InOrder(Tree root)
    375.  
      {
    376.  
      if (root==NULL)
    377.  
      return;
    378.  
      InOrder(root->lchild);
    379.  
      printf("%d ",root->val);
    380.  
      InOrder(root->rchild);
    381.  
      }
    382.  
      int main()
    383.  
      {
    384.  
      Tree root=NULL;
    385.  
      root = Insert(root, 16);
    386.  
      root = Insert(root, 3);
    387.  
       
    388.  
      //插入7后LR调整
    389.  
      root = Insert(root, 7);
    390.  
      root = Insert(root, 11);
    391.  
       
    392.  
      //插入9后LL调整
    393.  
      root = Insert(root, 9);
    394.  
       
    395.  
      //插入26后RR调整
    396.  
      root = Insert(root, 26);
    397.  
       
    398.  
      //插入28后RL调整
    399.  
      root = Insert(root, 18);
    400.  
      root = Insert(root, 14);
    401.  
       
    402.  
      //插入15后LR调整
    403.  
      root = Insert(root, 15);
    404.  
       
    405.  
      printf("插入: ");
    406.  
      printf("前序:");
    407.  
      PreOrder(root); // 11 7 3 9 18 15 14 16 26
    408.  
      printf(" ");
    409.  
       
    410.  
       
    411.  
      printf("中序:");
    412.  
      InOrder(root); // 3 7 9 11 14 15 16 18 26
    413.  
      printf(" ");
    414.  
       
    415.  
       
    416.  
      printf("删除: ");
    417.  
       
    418.  
      //测试删除叶子结点
    419.  
      // remove(root, 16);
    420.  
       
    421.  
       
    422.  
      //测试删除只有左孩子的结点
    423.  
      // remove(root, 16);
    424.  
      // remove(root, 15);
    425.  
       
    426.  
       
    427.  
      //测试删除只有右孩子的结点
    428.  
      // remove(root, 14);
    429.  
      // remove(root, 15);
    430.  
       
    431.  
      //测试删除有左右孩子的结点
    432.  
      // remove(root, 18);
    433.  
       
    434.  
      //删除26后进行LR型调整
    435.  
      remove(root, 26);
    436.  
       
    437.  
       
    438.  
      //删除18后进行RR型
    439.  
      remove(root, 18);
    440.  
       
    441.  
       
    442.  
      remove(root, 3);
    443.  
      remove(root, 9);
    444.  
       
    445.  
      //删除7过进行RL调整
    446.  
      remove(root, 7);
    447.  
       
    448.  
       
    449.  
      //删除11后进行LL调整
    450.  
      remove(root, 11);
    451.  
       
    452.  
      //把结点删除完
    453.  
      // remove(root, 15);
    454.  
      // remove(root, 14);
    455.  
      // remove(root, 16);
    456.  
       
    457.  
      printf("前序:");
    458.  
      PreOrder(root);
    459.  
      printf(" ");
    460.  
      printf("中序:");
    461.  
      InOrder(root);
    462.  
      printf(" ");
    463.  
      return 0;
    464.  
      }

    程序我反复测试过,目前还没有发现问题,当然也是还存在bug,不管是代码还是思考过程,如果有问题,还希望各位不吝指教。感谢。

    main函数太长难过,最后运行结果:

    三、平衡树的3+4重构

    平衡二叉树也是一颗BST树,那么BST的特点,平衡二叉树也应该具有,那么BST树又有什么特点呢,特点是有序,什么有序呢,中序遍历序列有序,也就是说BST树的中序序列是单调的,这个很容易就证明了,对根节点而已,其左子树的肯定比它本身要小,其右子树的值肯定比其值要大,而中序序列是左根右,所以BST树的中序序列肯定是单调的。这个是非常重要的性质,稍后我们将用到它。

    我们设g(x)为最低的失衡结点,考察祖孙三代:g~p~v,按照中序遍历次序将其重命名为:a<b<c。

    它们总共拥有互不相交的四颗(可能为空的)子树,按照中序遍历次序,将其重命名为:T0<T1<T2<T3。

    此时,如果我们依然按照中序的遍历次序将这两个序列混合起来,就可以得到一个长度为7的序列。在这个序列中三个结点a,b,c,必然是镶嵌于这4棵子树之间

    实际上无论是哪种具体的情况,经过这样的重命名之后,按照中序遍历的次序,必然是从T0到a,再从a到T1,再从T1到b,然后从b到T2,再从T2到c,最终由c到T3,这就是BST单调性的体现

    因此我们可以统一的将这三个顶点abc以及这4棵子树,按照下面的拓扑关系直接的拼接起来,这样的一种拼接是针对于三个结点,以及下属的4棵子树而言的,所以也称作3+4重构

    无论是插入还是删除,无论是单旋还是双旋,最终的效果都应该是这样一种形式。

    下面我们给出我们的3+4重构函数的代码:

    1.  
      //3+4重构函数
    2.  
      //参数:见分析
    3.  
      //返回:新根
    4.  
      Tree connect34(Tree &a,Tree &b,Tree &c,Tree &T0,Tree &T1,Tree &T2,Tree &T3)
    5.  
      {
    6.  
      a->lchild=T0;
    7.  
      if (T0)
    8.  
      T0->parent=a;
    9.  
      a->rchild=T1;
    10.  
      if(T1)
    11.  
      T1->parent=a;
    12.  
      update_depth(a);
    13.  
      c->lchild=T2;
    14.  
      if(T2)
    15.  
      T2->parent=c;
    16.  
      c->rchild=T3;
    17.  
      if(T3)
    18.  
      T3->parent=c;
    19.  
      update_depth(c);
    20.  
      b->lchild=a;
    21.  
      a->parent=b;
    22.  
      b->rchild=c;
    23.  
      c->parent=b;
    24.  
      update_depth(b);
    25.  
      return b;
    26.  
      }


    可以看见很简单,从此以后我们就不用各种旋转了,因此对于调整AVL树结构的函数,也需要改,不过我们就再原来的基础上调整一下就就可以了。

    1.  
      Tree rotateAt(Tree &root,Tree &node)
    2.  
      {
    3.  
      Tree son,temp;
    4.  
      Tree grandson;
    5.  
      int balance=0; //平衡因子
    6.  
      while (node!=NULL) //检查其祖先是否需要调整,更新
    7.  
      {
    8.  
      update_depth(node); //更新当前结点的高度信息
    9.  
      balance=is_balance(node); //获取当前结点的平衡因子情况
    10.  
      if (balance>1 || balance<-1) //平衡因子超标
    11.  
      {
    12.  
      if (balance>1) //左子树高
    13.  
      {
    14.  
      if (is_balance(node->lchild)>0) //LL型
    15.  
      {
    16.  
      //找祖孙三代,后面的类似
    17.  
      son=node->lchild; //找其左孩子
    18.  
      grandson=son->lchild; //找其左孩子的左孩子
    19.  
      son->parent=node->parent; //设置更新后的son的父节点
    20.  
      temp=node;
    21.  
      //重构
    22.  
      node=connect34(grandson, son, node, grandson->lchild, grandson->rchild, son->rchild, node->rchild);
    23.  
      setchild(son, temp, node);//设置son父节点的孩子为node
    24.  
      }
    25.  
      else //LR型
    26.  
      {
    27.  
      son=node->lchild;
    28.  
      grandson=son->rchild;
    29.  
      grandson->parent=node->parent;
    30.  
      temp=node;
    31.  
      node=connect34(son, grandson, node, son->lchild, grandson->lchild, grandson->rchild, node->rchild);
    32.  
      setchild(grandson, temp, node); //设置grandson父节点的孩子为node
    33.  
      }
    34.  
      }
    35.  
      else //右子树高
    36.  
      {
    37.  
      if (is_balance(node->rchild)<0) //RR型
    38.  
      {
    39.  
      son=node->rchild;
    40.  
      grandson=son->rchild;
    41.  
      son->parent=node->parent;
    42.  
      temp=node;
    43.  
      node=connect34(node, son, grandson, node->lchild, son->lchild, grandson->lchild, grandson->rchild);
    44.  
      setchild(son, temp, node); //设置son父节点的孩子为node
    45.  
      }
    46.  
      else //RL型
    47.  
      {
    48.  
      son=node->rchild;
    49.  
      grandson=son->lchild;
    50.  
      grandson->parent=node->parent;
    51.  
      temp=node;
    52.  
      node=connect34(node, grandson, son, node->lchild, grandson->lchild, grandson->rchild, son->rchild);
    53.  
       
    54.  
      setchild(grandson, temp, node); //设置grandson父节点的孩子为node
    55.  
       
    56.  
      }
    57.  
      }
    58.  
      if (node->parent==NULL) //到达根结点
    59.  
      {
    60.  
      root=node; //设置新的根结点
    61.  
      break; //退出
    62.  
      }
    63.  
      }
    64.  
      node=node->parent; //依次找到其父节点
    65.  
       
    66.  
      }
    67.  
      return root; //返回新根
    68.  
      }


    额,看着好复杂的样子,其实仔细观察,不难看出,我们只是在原来的基础上删除了旋转操作,然后把3+4重构函数加了进来,在进行重构之前,首先需要找到abc,以及T0,T1,T2,T3,这个看一下程序就明白了,不够值得注意的是,要重新设置它们的连接关系,尤其是abc,后它们祖先的关系,这里还有一个setchild函数,就是用来连接abc和它们祖先的函数,abc的拓扑在变,也就是最后son可能就成parent了,所以需要更新son的父节点。

    1.  
      void setchild(Tree &g,Tree &temp,Tree &node)
    2.  
      {
    3.  
      if (g->parent)
    4.  
      {
    5.  
      if (g->parent->lchild==temp)
    6.  
      g->parent->lchild=node;
    7.  
      else
    8.  
      g->parent->rchild=node;
    9.  
      }
    10.  
      }


    OK,这就是setchild函数,到这里平衡二叉树的3+4重构就讲完了,简单吧,下面给出其总代码,测试方式同上面的旋转的平衡二叉树。

    1.  
      #include <stdio.h>
    2.  
      #include <stdlib.h>
    3.  
      #include <iostream>
    4.  
      using namespace std;
    5.  
      typedef struct AVLNode *Tree;
    6.  
      typedef int ElementType;
    7.  
      struct AVLNode
    8.  
      {
    9.  
      int depth; //深度,这里计算每个结点的深度,通过深度的比较可得出是否平衡
    10.  
      Tree parent; //该结点的父节点,方便操作
    11.  
      ElementType val; //结点值
    12.  
      Tree lchild;
    13.  
      Tree rchild;
    14.  
      AVLNode(int val=0) //默认构造函数
    15.  
      {
    16.  
      parent=NULL;
    17.  
      depth=0;
    18.  
      lchild=rchild=NULL;
    19.  
      this->val=val;
    20.  
      }
    21.  
      };
    22.  
      Tree insert_val(Tree&,Tree,Tree);
    23.  
      Tree remove(Tree&,ElementType);
    24.  
      Tree remove_val(Tree &,Tree &);
    25.  
      void update_depth(Tree);
    26.  
      int get_balance(Tree);
    27.  
      int is_balance(Tree);
    28.  
      Tree *Find_Min(Tree&);
    29.  
      Tree connect34(Tree&,Tree&,Tree&,Tree&,Tree&,Tree&,Tree&);
    30.  
      Tree rotateAt(Tree&,Tree&);
    31.  
      void setchild(Tree &,Tree &,Tree &);
    32.  
       
    33.  
      //向AVL树中插入val
    34.  
      //参数:根,插入数据value
    35.  
      //返回:新根结点
    36.  
      Tree Insert(Tree &root,ElementType val)
    37.  
      {
    38.  
      Tree temp=NULL;
    39.  
      Tree node=new AVLNode(val);
    40.  
       
    41.  
      //插入结点
    42.  
      temp=insert_val(root,node,NULL); //调用真正的插入函数
    43.  
       
    44.  
      if (temp)
    45.  
      {
    46.  
      update_depth(temp);
    47.  
      root=rotateAt(root, temp);//检查树是否该调整
    48.  
      }
    49.  
      else //无需插入,释放结点
    50.  
      delete temp;
    51.  
      return root;
    52.  
      }
    53.  
      //插入函数
    54.  
      //参数:根节点,待插结点,待插结点的父节点
    55.  
      //返回:插入结点
    56.  
      Tree insert_val(Tree &root,Tree node,Tree parent)
    57.  
      {
    58.  
      if (root==NULL)
    59.  
      {
    60.  
      root=node;
    61.  
      node->parent=parent; //设置当前结点的父结点
    62.  
      return root; //返回插入结点
    63.  
      }
    64.  
      if (node->val<root->val) //插左子树
    65.  
      return insert_val(root->lchild, node,root);
    66.  
      else if(node->val>root->val) //插右子树
    67.  
      return insert_val(root->rchild, node,root);
    68.  
      else //已存在该结点,停止插入操作,返回NULL
    69.  
      return NULL;
    70.  
      }
    71.  
      //3+4重构函数
    72.  
      //参数:见分析
    73.  
      //返回:新根
    74.  
      Tree connect34(Tree &a,Tree &b,Tree &c,Tree &T0,Tree &T1,Tree &T2,Tree &T3)
    75.  
      {
    76.  
      a->lchild=T0;
    77.  
      if (T0)
    78.  
      T0->parent=a;
    79.  
      a->rchild=T1;
    80.  
      if(T1)
    81.  
      T1->parent=a;
    82.  
      update_depth(a);
    83.  
      c->lchild=T2;
    84.  
      if(T2)
    85.  
      T2->parent=c;
    86.  
      c->rchild=T3;
    87.  
      if(T3)
    88.  
      T3->parent=c;
    89.  
      update_depth(c);
    90.  
      b->lchild=a;
    91.  
      a->parent=b;
    92.  
      b->rchild=c;
    93.  
      c->parent=b;
    94.  
      update_depth(b);
    95.  
      return b;
    96.  
      }
    97.  
       
    98.  
      Tree rotateAt(Tree &root,Tree &node)
    99.  
      {
    100.  
      Tree son,temp;
    101.  
      Tree grandson;
    102.  
      int balance=0; //平衡因子
    103.  
      while (node!=NULL) //检查其祖先是否需要调整,更新
    104.  
      {
    105.  
      update_depth(node); //更新当前结点的高度信息
    106.  
      balance=is_balance(node); //获取当前结点的平衡因子情况
    107.  
      if (balance>1 || balance<-1) //平衡因子超标
    108.  
      {
    109.  
      if (balance>1) //左子树高
    110.  
      {
    111.  
      if (is_balance(node->lchild)>0) //LL型
    112.  
      {
    113.  
      //找祖孙三代,后面的类似
    114.  
      son=node->lchild; //找其左孩子
    115.  
      grandson=son->lchild; //找其左孩子的左孩子
    116.  
      son->parent=node->parent; //设置更新后的son的父节点
    117.  
      temp=node;
    118.  
      //重构
    119.  
      node=connect34(grandson, son, node, grandson->lchild, grandson->rchild, son->rchild, node->rchild);
    120.  
      setchild(son, temp, node);//设置son父节点的孩子为node
    121.  
      }
    122.  
      else //LR型
    123.  
      {
    124.  
      son=node->lchild;
    125.  
      grandson=son->rchild;
    126.  
      grandson->parent=node->parent;
    127.  
      temp=node;
    128.  
      node=connect34(son, grandson, node, son->lchild, grandson->lchild, grandson->rchild, node->rchild);
    129.  
      setchild(grandson, temp, node); //设置grandson父节点的孩子为node
    130.  
      }
    131.  
      }
    132.  
      else //右子树高
    133.  
      {
    134.  
      if (is_balance(node->rchild)<0) //RR型
    135.  
      {
    136.  
      son=node->rchild;
    137.  
      grandson=son->rchild;
    138.  
      son->parent=node->parent;
    139.  
      temp=node;
    140.  
      node=connect34(node, son, grandson, node->lchild, son->lchild, grandson->lchild, grandson->rchild);
    141.  
      setchild(son, temp, node); //设置son父节点的孩子为node
    142.  
      }
    143.  
      else //RL型
    144.  
      {
    145.  
      son=node->rchild;
    146.  
      grandson=son->lchild;
    147.  
      grandson->parent=node->parent;
    148.  
      temp=node;
    149.  
      node=connect34(node, grandson, son, node->lchild, grandson->lchild, grandson->rchild, son->rchild);
    150.  
       
    151.  
      setchild(grandson, temp, node); //设置grandson父节点的孩子为node
    152.  
       
    153.  
      }
    154.  
      }
    155.  
      if (node->parent==NULL) //到达根结点
    156.  
      {
    157.  
      root=node; //设置新的根结点
    158.  
      break; //退出
    159.  
      }
    160.  
      }
    161.  
      node=node->parent; //依次找到其父节点
    162.  
       
    163.  
      }
    164.  
      return root; //返回新根
    165.  
      }
    166.  
      void setchild(Tree &g,Tree &temp,Tree &node)
    167.  
      {
    168.  
      if (g->parent)
    169.  
      {
    170.  
      if (g->parent->lchild==temp)
    171.  
      g->parent->lchild=node;
    172.  
      else
    173.  
      g->parent->rchild=node;
    174.  
      }
    175.  
      }
    176.  
      //查找最小结点
    177.  
      Tree *Find_Min(Tree &root)
    178.  
      {
    179.  
      if (root->lchild)
    180.  
      {
    181.  
      return Find_Min(root->lchild);
    182.  
      }
    183.  
      return &root;
    184.  
      }
    185.  
       
    186.  
      //删除操作
    187.  
      //参数:根,需要删除的结点
    188.  
      //返回值: 返回删除结点的父节点
    189.  
      Tree remove_val(Tree &root,Tree &node)
    190.  
      {
    191.  
      Tree parent=node->parent;
    192.  
      Tree temp=NULL;
    193.  
      //只有左孩子
    194.  
      if (node->rchild==NULL && node->lchild!=NULL)
    195.  
      {
    196.  
      temp=node;
    197.  
      node=node->lchild; //指向左孩子
    198.  
      node->parent=temp->parent;
    199.  
      delete temp; //释放结点
    200.  
      update_depth(node); //更新当前结点信息
    201.  
      }
    202.  
      else if(node->lchild==NULL && node->rchild!=NULL) //只有右孩子
    203.  
      {
    204.  
      temp=node;
    205.  
      node=node->rchild; //指向右结点
    206.  
      node->parent=temp->parent;
    207.  
      delete temp; //释放结点
    208.  
      update_depth(node); //更新当前结点信息
    209.  
      }
    210.  
      else if(node->rchild==NULL && node->lchild==NULL) //叶子结点
    211.  
      {
    212.  
      parent=node->parent; //找到其父节点
    213.  
      if (parent) //如果父节点存在
    214.  
      {
    215.  
      delete node;
    216.  
      node=NULL;
    217.  
      update_depth(parent); //更新父节点高度信息
    218.  
      }
    219.  
      else //删除的是根
    220.  
      {
    221.  
      delete root;
    222.  
      root=NULL;
    223.  
      }
    224.  
      }
    225.  
      else //既有左孩子也有右孩子,化繁为简
    226.  
      {
    227.  
      Tree *tmp=Find_Min(node->rchild); //找到替代元素,temp为叶子结点
    228.  
      node->val=(*tmp)->val; //更新值
    229.  
      //判断当前叶子结点是左孩子还是右孩子。
    230.  
      parent=(*tmp)->parent;
    231.  
      delete *tmp;
    232.  
      *tmp=NULL;
    233.  
      update_depth(parent);
    234.  
      }
    235.  
      return parent;
    236.  
      }
    237.  
       
    238.  
      //找到删除的结点,执行删除操作,并根据情况调整AVL树
    239.  
      //参数:根,需要删除的val
    240.  
      //返回:找到删除结点的情况则返回新根,否则返回NULL
    241.  
      Tree remove(Tree &root,ElementType val)
    242.  
      {
    243.  
      static Tree *temp=NULL;
    244.  
      if (root==NULL)
    245.  
      {
    246.  
      temp=NULL;
    247.  
      return NULL;
    248.  
      }
    249.  
      else if(root->val<val) //在右子树查找
    250.  
      remove(root->rchild, val);
    251.  
      else if(root->val>val) //在左子树查找
    252.  
      remove(root->lchild, val);
    253.  
      else //找到了,标记一下
    254.  
      temp=&root;
    255.  
       
    256.  
      if (temp)
    257.  
      {
    258.  
      if (!root->parent) //如果已经返回到最后一次(也就是root是真正的树根)
    259.  
      {
    260.  
      Tree tmp=NULL;
    261.  
      tmp=remove_val(root,*temp); //执行删除操作
    262.  
      return rotateAt(root, tmp);
    263.  
      }
    264.  
      return *temp;
    265.  
      }
    266.  
      return NULL;
    267.  
      }
    268.  
       
    269.  
      //获取当前结点的深度
    270.  
      int get_balance(Tree node)
    271.  
      {
    272.  
      if (node==NULL)
    273.  
      return 0;
    274.  
      return node->depth;
    275.  
      }
    276.  
      //返回当前平衡因子
    277.  
      int is_balance(Tree node)
    278.  
      {
    279.  
      if (node==NULL)
    280.  
      return 0;
    281.  
      else
    282.  
      return get_balance(node->lchild)-get_balance(node->rchild);
    283.  
      }
    284.  
       
    285.  
      //更新当前深度
    286.  
      void update_depth(Tree node)
    287.  
      {
    288.  
      if (node==NULL)
    289.  
      return;
    290.  
      else
    291.  
      {
    292.  
      int depth_Lchild=get_balance(node->lchild); //左孩子深度
    293.  
      int depth_Rchild=get_balance(node->rchild); //右孩子深度
    294.  
      node->depth=max(depth_Lchild,depth_Rchild)+1;
    295.  
      }
    296.  
      }
    297.  
      //前序
    298.  
      void PreOrder(Tree root)
    299.  
      {
    300.  
      if (root==NULL)
    301.  
      return;
    302.  
      printf("%d ",root->val);
    303.  
      PreOrder(root->lchild);
    304.  
      PreOrder(root->rchild);
    305.  
      }
    306.  
      //中序
    307.  
      void InOrder(Tree root)
    308.  
      {
    309.  
      if (root==NULL)
    310.  
      return;
    311.  
      InOrder(root->lchild);
    312.  
      printf("%d ",root->val);
    313.  
      InOrder(root->rchild);
    314.  
      }
    315.  
      int main()
    316.  
      {
    317.  
      Tree root=NULL;
    318.  
      root = Insert(root, 16);
    319.  
      root = Insert(root, 3);
    320.  
       
    321.  
      //插入7后LR调整
    322.  
      root = Insert(root, 7);
    323.  
      root = Insert(root, 11);
    324.  
       
    325.  
      //插入9后LL调整
    326.  
      root = Insert(root, 9);
    327.  
       
    328.  
      //插入26后RR调整
    329.  
      root = Insert(root, 26);
    330.  
       
    331.  
      //插入18后RL调整
    332.  
      root = Insert(root, 18);
    333.  
      root = Insert(root, 14);
    334.  
       
    335.  
      //插入15后LR调整
    336.  
      root = Insert(root, 15);
    337.  
       
    338.  
      printf("插入: ");
    339.  
      printf("前序:");
    340.  
      PreOrder(root); // 11 7 3 9 18 15 14 16 26
    341.  
      printf(" ");
    342.  
       
    343.  
       
    344.  
      printf("中序:");
    345.  
      InOrder(root); // 3 7 9 11 14 15 16 18 26
    346.  
      printf(" ");
    347.  
       
    348.  
       
    349.  
      printf("删除: ");
    350.  
       
    351.  
      //测试删除叶子结点
    352.  
      // remove(root, 16);
    353.  
       
    354.  
       
    355.  
      //测试删除只有左孩子的结点
    356.  
      // remove(root, 16);
    357.  
      // remove(root, 15);
    358.  
       
    359.  
       
    360.  
      //测试删除只有右孩子的结点
    361.  
      // remove(root, 14);
    362.  
      // remove(root, 15);
    363.  
       
    364.  
      //测试删除有左右孩子的结点
    365.  
      // remove(root, 18);
    366.  
       
    367.  
      //删除26后进行LR型调整
    368.  
      remove(root, 26);
    369.  
       
    370.  
       
    371.  
      //删除18后进行RR型
    372.  
      remove(root, 18);
    373.  
       
    374.  
       
    375.  
      remove(root, 3);
    376.  
      remove(root, 9);
    377.  
       
    378.  
      //删除7过进行RL调整
    379.  
      remove(root, 7);
    380.  
       
    381.  
       
    382.  
      //删除11后进行LL调整
    383.  
      remove(root, 11);
    384.  
       
    385.  
      //把结点删除完
    386.  
      // remove(root, 15);
    387.  
      // remove(root, 14);
    388.  
      // remove(root, 16);
    389.  
       
    390.  
      printf("前序:");
    391.  
      PreOrder(root);
    392.  
      printf(" ");
    393.  
      printf("中序:");
    394.  
      InOrder(root);
    395.  
      printf(" ");
    396.  
      return 0;
    397.  
      }

    程序我反复测试过,目前还没有发现问题,当然也是还存在bug,不管是代码还是思考过程,如果有问题,还希望各位不吝指教。感谢。

    注:程序中大量使用了引用和指针,如果对指针有疑惑的可以参考C语言指针初探

    欢迎继续查看伸展树图解与实现二叉搜索树

  • 相关阅读:
    WinForm事件中的Object sender和EventArgs e参数
    Day 25:Python 模块 collections 3 个常用类
    Day 23:Python 中的一些高频面试题及解答小记
    Day 22:Python 迭代器和生成器小记
    Day 21:Python 多线程和协程
    Day 20:python中几个常用的内置函数
    Day 19:Python 函数五类参数
    遍历容器auto方法
    JS鼠标提示框效果
    数据库(二)
  • 原文地址:https://www.cnblogs.com/LiuYanYGZ/p/13695554.html
Copyright © 2011-2022 走看看