rotations.java

来自「Data StructuresAnd Algorithm Analysis In」· Java 代码 · 共 51 行

JAVA
51
字号
    package DataStructures;

    final class Rotations
    {
        /**
         * Rotate binary tree node with left child.
         * For AVL trees, this is a single rotation for case 1.
         */
        static BinaryNode withLeftChild( BinaryNode k2 )
        {
            BinaryNode k1 = k2.left;
            k2.left = k1.right;
            k1.right = k2;
            return k1;
        }

        /**
         * Rotate binary tree node with right child.
         * For AVL trees, this is a single rotation for case 4.
         */
        static BinaryNode withRightChild( BinaryNode k1 )
        {
            BinaryNode k2 = k1.right;
            k1.right = k2.left;
            k2.left = k1;
            return k2;
        }

        /**
         * 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.
         */
        static BinaryNode doubleWithLeftChild( BinaryNode k3 )
        {
            k3.left = withRightChild( k3.left );
            return withLeftChild( k3 );
        }

        /**
         * 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.
         */
        static BinaryNode doubleWithRightChild( BinaryNode k1 )
        {
            k1.right = withLeftChild( k1.right );
            return withRightChild( k1 );
        }
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?