• AVL树


    typedef struct AVLNode *Position;
    typedef Position AVLTree; /* AVL树类型 */
    struct AVLNode{
        ElementType Data; /* 结点数据 */
        AVLTree Left;     /* 指向左子树 */
        AVLTree Right;    /* 指向右子树 */
        int Height;       /* 树高 */
    };
     
    int Max ( int a, int b )
    {
        return a > b ? a : b;
    }
     
    AVLTree SingleLeftRotation ( AVLTree A )
    { /* 注意:A必须有一个左子结点B */
      /* 将A与B做左单旋,更新A与B的高度,返回新的根结点B */     
     
        AVLTree B = A->Left;
        A->Left = B->Right;
        B->Right = A;
        A->Height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1;
        B->Height = Max( GetHeight(B->Left), A->Height ) + 1;
      
        return B;
    }
     
    AVLTree DoubleLeftRightRotation ( AVLTree A )
    { /* 注意:A必须有一个左子结点B,且B必须有一个右子结点C */
      /* 将A、B与C做两次单旋,返回新的根结点C */
         
        /* 将B与C做右单旋,C被返回 */
        A->Left = SingleRightRotation(A->Left);
        /* 将A与C做左单旋,C被返回 */
        return SingleLeftRotation(A);
    }
     
    /*************************************/
    /* 对称的右单旋与右-左双旋请自己实现 */
    /*************************************/
     
    AVLTree Insert( AVLTree T, ElementType X )
    { /* 将X插入AVL树T中,并且返回调整后的AVL树 */
        if ( !T ) { /* 若插入空树,则新建包含一个结点的树 */
            T = (AVLTree)malloc(sizeof(struct AVLNode));
            T->Data = X;
            T->Height = 0;
            T->Left = T->Right = NULL;
        } /* if (插入空树) 结束 */
     
        else if ( X < T->Data ) {
            /* 插入T的左子树 */
            T->Left = Insert( T->Left, X);
            /* 如果需要左旋 */
            if ( GetHeight(T->Left)-GetHeight(T->Right) == 2 )
                if ( X < T->Left->Data ) 
                   T = SingleLeftRotation(T);      /* 左单旋 */
                else 
                   T = DoubleLeftRightRotation(T); /* 左-右双旋 */
        } /* else if (插入左子树) 结束 */
         
        else if ( X > T->Data ) {
            /* 插入T的右子树 */
            T->Right = Insert( T->Right, X );
            /* 如果需要右旋 */
            if ( GetHeight(T->Left)-GetHeight(T->Right) == -2 )
                if ( X > T->Right->Data ) 
                   T = SingleRightRotation(T);     /* 右单旋 */
                else 
                   T = DoubleRightLeftRotation(T); /* 右-左双旋 */
        } /* else if (插入右子树) 结束 */
     
        /* else X == T->Data,无须插入 */
     
        /* 别忘了更新树高 */
        T->Height = Max( GetHeight(T->Left), GetHeight(T->Right) ) + 1;
         
        return T;
    }
    AVLTree Delete(AVLTree T, ElementType X)
    {
      if(!T)  // 根为空直接返回NULL
      {
        ERROR();
        return NULL;
      }
      else
      {
        if(X < T->Data)   // 待删除的节点在"左子树"中
        {
            T->Left = Delete(T->Left,X);
             // 删除节点后,若AVL树失去平衡,则进行相应的调节
            if(GetHeight(T->Left)-GetHeight(T->Right) == -2)
            {
               AVLTree Tmp = T->Right;
               if(GetHeight(Tmp->Left) > GetHeight(Tmp->Right))
                T = DoubleRightLeftRotation(T);
              else
                T = SingleRightRotation(T);
            }
        }
        else if(X > T->Data)
        {
           T->Right = Delete(T->Right,X);
           if(GetHeight(T->Left)-GetHeight(T->Right) == 2)
           {
               AVLTree Tmp2 = T->Left;
               if(GetHeight(Tmp2->Left) > GetHeight(Tmp2->Right))
                T = SingleLeftRotation(T);
               else 
                T = DoubleLeftRightRotation(T);
           }
        }
        else
        {
            if((T->Left) && (T->Right))  // 左右孩子都非空
            {
               if(GetHeight(T->Left) > GetHeight(T-<Right))
               {
                      // 如果tree的左子树比右子树高;
                      // 则(01)找出tree的左子树中的最大节点
                      //   (02)将该最大节点的值赋值给tree。
                      //   (03)删除该最大节点。
                      // 这类似于用"tree的左子树中最大节点"做"tree"的替身;
                      // 采用这种方式的好处是:删除"tree的左子树中最大节点"之后,AVL树仍然是平衡的。
                      AVLTree max = FindMax(T->Left);
                      T->Data = max->Data;
                      T->Left = Delete(T->Left,T->Data);
               }
               else
               {
                      AVLTree min = FindMin(T->Right);
                      T->Data = min->Data;
                      T->Right = Delete(T->Right,T->Data);
               }
            }
            else
            {
                AVLTree Tmp3 = T;
                T = T->Left ? T->Left : T->Right;
                free(Tmp3);
            }
        }
      }
    }
  • 相关阅读:
    ubuntu安装与卸载java
    linux ubuntu 用户名,主机名,密码修改,增加用户,删除用户
    linux中sudo fdisk -l报错:GPT PMBR size mismatch will be corrected by write错误
    VM VirtualBox虚拟机vdi扩大磁盘空间容量
    WinSCP传输文件到虚拟机linux报错:SSH2_MSG_CHANNEL_FAILURE for nonexistent channel 0
    parallel python多进程集群模式
    zookeeper报错:ERROR [main:QuorumPeerMain@86]
    hive启动报错:Exception in thread "main" java.lang.RuntimeException: com.ctc.wstx.exc.WstxParsingException: Illegal character entity: expansion character (code 0x8 at
    3.数据链路层
    2.物理层
  • 原文地址:https://www.cnblogs.com/dzy521/p/9536772.html
Copyright © 2020-2023  润新知