protected override void Remove(BinarySearchTreeNode <T> node)
        {
            if (node.HasRight && node.HasLeft)
            {
                var leftHeight    = node.Left?.Height ?? 0;
                var rightHeight   = node.Right?.Height ?? 0;
                var nodeToReplace = leftHeight > rightHeight
                    ? this.GetPredecessor(node.Content)
                    : this.GetSuccessor(node.Content);

                node.Content = nodeToReplace.Content;
                node         = nodeToReplace;
            }

            // has 1 or 0 child
            var redBlackNode = node as RedBlackTreeNode <T>;

            // if red and has no more than 1 child - it hasn't child
            if (redBlackNode.IsRed)
            {
                this.RemoveLeaf(node);
                return;
            }

            // node is black
            var child = (node.HasLeft
                ? node.Left
                : node.Right) as RedBlackTreeNode <T>;

            // if black node has only 1 red child - copy content and remove child
            // (red child cannot contain any nodes)
            if (child != null && child.IsRed)
            {
                node.Content = child.Content;
                this.RemoveLeaf(child);
                return;
            }

            // node is black and no childs (black node cannot has just 1 black child)
            this.UpdateColorOnRemove(redBlackNode);
            this.RemoveLeaf(node);
        }
示例#2
0
        protected override void Remove(BinarySearchTreeNode <T> node)
        {
            if (node.IsTerminate)
            {
                var parentNode = node.Parent as AVLTreeNode <T>;
                this.RemoveLeaf(node);
                this.RebalanceIn(parentNode as AVLTreeNode <T>);
            }
            else
            {
                var leftHeight    = node.Left?.Height ?? -1;
                var rightHeight   = node.Right?.Height ?? -1;
                var nodeToReplace = leftHeight > rightHeight
                    ? this.GetPredecessor(node.Content)
                    : this.GetSuccessor(node.Content);

                node.Content = nodeToReplace.Content;
                this.Remove(nodeToReplace);
            }
        }
示例#3
0
        /// <summary>
        /// Verifying AVL tree node
        /// </summary>
        /// <param name="node">Tree node</param>
        /// <param name="min">Tree min node</param>
        /// <param name="max">Tree max node</param>
        /// <returns>True if AVL tree node is valid</returns>
        protected override bool VerifyIn(BinarySearchTreeNode <T> node, BinarySearchTreeNode <T> min = null, BinarySearchTreeNode <T> max = null)
        {
            var avlNode = node as AVLTreeNode <T>;

            return(avlNode.CalculatedHeight == avlNode.Height &&
                   Math.Abs(avlNode.BalanceFactor) < 2 &&
                   base.VerifyIn(node, min, max));
        }
        /// <summary>
        /// Verifying RB tree node
        /// </summary>
        /// <param name="node">Tree node</param>
        /// <param name="min">Tree min node</param>
        /// <param name="max">Tree max node</param>
        /// <returns>True if RB tree node is valid</returns>
        protected override bool VerifyIn(BinarySearchTreeNode <T> node, BinarySearchTreeNode <T> min = null, BinarySearchTreeNode <T> max = null)
        {
            var redBlackNode = node as RedBlackTreeNode <T>;
            var left         = node.Left as RedBlackTreeNode <T>;
            var right        = node.Right as RedBlackTreeNode <T>;
            var root         = this.Root as RedBlackTreeNode <T>;

            var isColorMatching = redBlackNode.IsRed
                ? ((!node.HasLeft || left.IsBlack) && (!node.HasRight || right.IsBlack))
                : true;

            return(root.IsBlack &&
                   isColorMatching &&
                   (left?.BlackHeight ?? 1) == (right?.BlackHeight ?? 1) &&
                   base.VerifyIn(node, min, max));
        }