Beispiel #1
0
        /// <summary>
        /// Finds the min node in the subtree and returns the root of the new subtree
        /// </summary>
        private BinarySearchTreeNode <T> FindAndRemoveMin(BinarySearchTreeNode <T> subtreeRoot, ref BinarySearchTreeNode <T> min)
        {
            if (subtreeRoot.Left == null)
            {
                if (subtreeRoot.Right == null)
                {
                    min = subtreeRoot;
                    return(null);
                }
                else
                {
                    min = subtreeRoot;
                    return(subtreeRoot.Right);
                }
            }

            var curNode  = subtreeRoot;
            var lastNode = subtreeRoot;

            while (curNode.Left != null)
            {
                lastNode = curNode;
                curNode  = curNode.Left;
            }

            lastNode.Left = curNode.Right;
            min           = curNode;

            return(subtreeRoot);
        }
Beispiel #2
0
        /// <summary>
        /// Removes an element from the <see cref="BinarySearchTree{T}"/>.
        /// </summary>
        /// <param name="value">The value to remove.</param>
        /// <returns>true if the item is successfully removed; otherwise false. Also returns false if item is not found.</returns>
        public virtual bool Remove(T value)
        {
            if (Root == null)
            {
                return(false);
            }

            var  curNode         = Root;
            var  lastNode        = Root;
            bool lastWasLeftSide = true;

            while (curNode != null)
            {
                int cmp = comparer.Compare(value, curNode.Value);

                if (cmp < 0)
                {
                    lastNode        = curNode;
                    curNode         = curNode.Left;
                    lastWasLeftSide = true;
                }
                else if (cmp > 0)
                {
                    lastNode        = curNode;
                    curNode         = curNode.Right;
                    lastWasLeftSide = false;
                }
                else
                {
                    if (curNode.Right == null)
                    {
                        if (lastWasLeftSide)
                        {
                            if (curNode == Root)
                            {
                                Root = curNode.Left;
                            }
                            else
                            {
                                lastNode.Left = curNode.Left;
                            }
                        }
                        else
                        {
                            lastNode.Right = curNode.Left;
                        }
                    }
                    else
                    {
                        BinarySearchTreeNode <T> min = null;
                        var rightNode = FindAndRemoveMin(curNode.Right, ref min);
                        min.Right = rightNode;
                        min.Left  = curNode.Left;

                        if (lastWasLeftSide)
                        {
                            if (curNode == Root)
                            {
                                Root = min;
                            }
                            else
                            {
                                lastNode.Left = min;
                            }
                        }
                        else
                        {
                            lastNode.Right = min;
                        }
                    }

                    curNode.Invalidate();
                    Count--;
                    return(true);
                }
            }
            return(false);
        }