示例#1
0
        /// <summary>
        /// Corects tree.
        /// </summary>
        /// <param name="node">Start fom this node.</param>
        private void RepairTree(AVLNode node)
        {
            AVLNode temp = node;

            //if (node == this.rootNode)
            //{
            //    return;
            //}



            while (temp.BalanceFactor <= 1 && temp.BalanceFactor >= -1)
            {
                if (temp.ParentNode != null)
                {
                    this.RepairTree(temp.ParentNode);
                }
                return;
            }

            if (temp.BalanceFactor > 1)
            {
                if (temp.LeftNode.BalanceFactor > -1)
                {
                    temp.RotateRight(this);
                    RepairTree(temp);
                    return;
                }

                temp.LeftNode.RotateLeft(this);
                temp.RotateRight(this);
                return;
            }

            if (temp.RightNode.BalanceFactor < 1)
            {
                temp.RotateLeft(this);
                return;
            }

            temp.RightNode.RotateRight(this);
            temp.RotateLeft(this);
            return;
        }
示例#2
0
        public void RotateRight_FullTree()
        {
            var node = new AVLNode <int>(100);

            node.Left  = new AVLNode <int>(50);
            node.Right = new AVLNode <int>(150);

            node.Left.Left   = new AVLNode <int>(25);
            node.Left.Right  = new AVLNode <int>(75);
            node.Right.Left  = new AVLNode <int>(125);
            node.Right.Right = new AVLNode <int>(175);

            node = node.RotateRight();

            Assert.AreEqual <int>(50, node.Value);
            Assert.AreEqual <int>(25, node.Left.Value);
            Assert.AreEqual <int>(100, node.Right.Value);
            Assert.AreEqual <int>(75, node.Right.Left.Value);
            Assert.AreEqual <int>(150, node.Right.Right.Value);
            Assert.AreEqual <int>(125, node.Right.Right.Left.Value);
            Assert.AreEqual <int>(175, node.Right.Right.Right.Value);
        }
示例#3
0
        public void RotateRight_FullTree()
        {
            var node = new AVLNode<int>(100);

            node.Left = new AVLNode<int>(50);
            node.Right = new AVLNode<int>(150);

            node.Left.Left = new AVLNode<int>(25);
            node.Left.Right = new AVLNode<int>(75);
            node.Right.Left = new AVLNode<int>(125);
            node.Right.Right = new AVLNode<int>(175);

            node = node.RotateRight();

            Assert.AreEqual<int>(50, node.Value);
            Assert.AreEqual<int>(25, node.Left.Value);
            Assert.AreEqual<int>(100, node.Right.Value);
            Assert.AreEqual<int>(75, node.Right.Left.Value);
            Assert.AreEqual<int>(150, node.Right.Right.Value);
            Assert.AreEqual<int>(125, node.Right.Right.Left.Value);
            Assert.AreEqual<int>(175, node.Right.Right.Right.Value);
        }