• AVL tree rotate


    AVL tree
    ----------------------------------------------------------------------------
    rotate
    /**
     * Rotate binary tree node with left child.
     * For AVL trees, this is a single rotation for case 1.
     * Update heights, then set new root.
     */
    void rotateWithLeftChild( AvlNode * & k2 )
    {
        AvlNode *k1 = k2->left;
        k2->left = k1->right;
        k1->right = k2;
        k2->height = max( height( k2->left ), height( k2->right ) ) + 1;
        k1->height = max( height( k1->left ), k2->height ) + 1;
        k2 = k1;
    }
        
        k2
       / \
      k1  z
     / \
    x   y
    |
    -->
      k1
     / \
    x   k2
    |  / \
      y   z

    ----------------------------------------------------------------------------
    /**
     * Rotate binary tree node with right child.
     * For AVL trees, this is a single rotation for case 4.
     * Update heights, then set new root.
     */
    void rotateWithRightChild( AvlNode * & k1 )
    {
        AvlNode *k2 = k1->right;
        k1->right = k2->left;
        k2->left = k1;
        k1->height = max( height( k1->left ), height( k1->right ) ) + 1;
        k2->height = max( height( k2->right ), k1->height ) + 1;
        k1 = k2;
    }

       k1
      / \
     X   k2
         / \
         y  z
            |
    -->
       k2
      /  \
      k1  z
     / \  |
    x   y

    ----------------------------------------------------------------------------
    /**
     * Double rotate binary tree node: first left child.
     * with its right child; then node k3 with new left child.
     * For AVL trees, this is a double rotation for case 2.
     * Update heights, then set new root.
     */
    void doubleWithLeftChild( AvlNode * & k3 )
    {
        rotateWithRightChild( k3->left );
        rotateWithLeftChild( k3 );
    }    
    2
        k3
       / \
      k1  d
     / \        
    a   k2  
       / \
      b   c
    -->
        k2
       /  \
      k1   k3
     / \   / \    
    a  b   c  d
    ----------------------------------------------------------------------------
    /**
     * Double rotate binary tree node: first right child.
     * with its left child; then node k1 with new right child.
     * For AVL trees, this is a double rotation for case 3.
     * Update heights, then set new root.
     */
    void doubleWithRightChild( AvlNode * & k1 )
    {
        rotateWithLeftChild( k1->right );
        rotateWithRightChild( k1 );
    }

    3
      k1
     / \
    a   k3
       / \    
      k2  d
     / \
    b   c
    -->
        k2
       /  \
      k1   k3
     / \   / \    
    a  b   c  d
  • 相关阅读:
    mysql数据创建带参的存储过程,并在存储过程中调用另一个存储过程
    python解析.xls/.xlsx文件--openpyxl模块(第三方)
    python使用django开发接口
    Mysql创建存储过程--批量插入数据
    Centos7下安装kafka,并使用python操作kafka的简单使用
    Centos7下安装JDK1.8
    Centos7下docker的安装
    python解析.xml文件-- xmltodict模块(第三方)
    解决:git SSL certificate problem: unable to get local issuer certificate
    AD域是什么意思?
  • 原文地址:https://www.cnblogs.com/BruceWayne09/p/16427700.html
Copyright © 2020-2023  润新知