예제 #1
0
        public static AVLnode LeftRotation(AVLnode node)
        {
            AVLnode root = node.RightChild;

            AVLnode pivot = root.LeftChild;

            root.LeftChild  = node;
            node.RightChild = pivot;

            return(root);
        }
예제 #2
0
        /**
         *  Main function
         */
        public static AVLnode InsertRecursive(AVLnode node, AVLnode root)
        {
            if (root == null)
            {
                return(node);
            }
            else
            {
                if (node.Value < root.Value)
                {
                    root.LeftChild = AVLtree.InsertRecursive(node, root.LeftChild);
                }
                else
                {
                    root.RightChild = AVLtree.InsertRecursive(node, root.RightChild);
                }
            }

            if (root.BalanceFactor > 1)
            {
                // leva stran poddrevesa
                if (node.Value < root.LeftChild.Value)
                {
                    root = AVLrotations.RightRotation(root);
                }
                else
                {
                    // desna stran poddrevesa
                    root.RightChild = AVLrotations.LeftRotation(root.RightChild);
                    root            = AVLrotations.RightRotation(root);
                }
            }
            else if (root.BalanceFactor < -1)
            {
                if (node.Value > root.RightChild.Value)
                {
                    root = AVLrotations.LeftRotation(root);
                }
                else
                {
                    root.RightChild = AVLrotations.RightRotation(root);
                    root            = AVLrotations.LeftRotation(root);
                }
            }

            // set balance factor
            return(root);
        }
예제 #3
0
 public AVLtree(AVLnode node)
 {
     this.Root = node;
 }
예제 #4
0
        public void InsertValue(int value)
        {
            AVLnode node = new AVLnode(value);

            this.Root = AVLtree.InsertRecursive(node, this.Root);
        }
예제 #5
0
 public static int GetBalanceFactor(AVLnode node)
 {
     return(node.LeftChild.Height - node.RightChild.Height);
 }