Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            AVLtree tree = new AVLtree();

            tree.InsertValue(10);
            tree.InsertValue(9);
            tree.InsertValue(8);
            tree.InsertValue(7);
            tree.InsertValue(6);
            tree.InsertValue(5);
            tree.InsertValue(4);
            tree.InsertValue(3);
            tree.InsertValue(2);
            tree.InsertValue(1);
            Console.WriteLine(tree);
        }
Ejemplo n.º 3
0
        public void InsertValue(int value)
        {
            AVLnode node = new AVLnode(value);

            this.Root = AVLtree.InsertRecursive(node, this.Root);
        }