Exemple #1
0
        private void FixTreeOnDelete(RedBlackTreeNode <T> node, RedBlackTreeNode <T> parentNode)
        {
            if (parentNode == null)
            {
                return;
            }
            RedBlackTreeNode <T> redBlackTreeNode = AugmentableRedBlackTree <T, Host> .Sibling(node, parentNode);

            if (redBlackTreeNode.color)
            {
                parentNode.color       = true;
                redBlackTreeNode.color = false;
                if (node != parentNode.left)
                {
                    this.RotateRight(parentNode);
                }
                else
                {
                    this.RotateLeft(parentNode);
                }
                redBlackTreeNode = AugmentableRedBlackTree <T, Host> .Sibling(node, parentNode);
            }
            if (!parentNode.color && !redBlackTreeNode.color && !AugmentableRedBlackTree <T, Host> .GetColor(redBlackTreeNode.left) && !AugmentableRedBlackTree <T, Host> .GetColor(redBlackTreeNode.right))
            {
                redBlackTreeNode.color = true;
                this.FixTreeOnDelete(parentNode, parentNode.parent);
                return;
            }
            if (parentNode.color && !redBlackTreeNode.color && !AugmentableRedBlackTree <T, Host> .GetColor(redBlackTreeNode.left) && !AugmentableRedBlackTree <T, Host> .GetColor(redBlackTreeNode.right))
            {
                redBlackTreeNode.color = true;
                parentNode.color       = false;
                return;
            }
            if (node == parentNode.left && !redBlackTreeNode.color && AugmentableRedBlackTree <T, Host> .GetColor(redBlackTreeNode.left) && !AugmentableRedBlackTree <T, Host> .GetColor(redBlackTreeNode.right))
            {
                redBlackTreeNode.color      = true;
                redBlackTreeNode.left.color = false;
                this.RotateRight(redBlackTreeNode);
            }
            else if (node == parentNode.right && !redBlackTreeNode.color && AugmentableRedBlackTree <T, Host> .GetColor(redBlackTreeNode.right) && !AugmentableRedBlackTree <T, Host> .GetColor(redBlackTreeNode.left))
            {
                redBlackTreeNode.color       = true;
                redBlackTreeNode.right.color = false;
                this.RotateLeft(redBlackTreeNode);
            }
            redBlackTreeNode = AugmentableRedBlackTree <T, Host> .Sibling(node, parentNode);

            redBlackTreeNode.color = parentNode.color;
            parentNode.color       = false;
            if (node == parentNode.left)
            {
                if (redBlackTreeNode.right != null)
                {
                    redBlackTreeNode.right.color = false;
                }
                this.RotateLeft(parentNode);
                return;
            }
            if (redBlackTreeNode.left != null)
            {
                redBlackTreeNode.left.color = false;
            }
            this.RotateRight(parentNode);
        }