예제 #1
0
        /// <summary>
        /// Removes element from the tree.
        /// </summary>
        /// <param name="data">
        /// Element to be removed.
        /// </param>
        /// <returns>
        /// The remove.
        /// </returns>
        public override bool Remove(T data)
        {
            if (this._root == null)
            {
                return(false);
            }

            BinarySearchTreeNode <T> current = this._root, parent = null;
            int result = this._comparer.Compare(current.Values[0], data);

            while (result != 0)
            {
                if (result > 0)
                {
                    parent  = current;
                    current = current.Left;
                }
                else
                {
                    parent  = current;
                    current = current.Right;
                }

                if (current == null)
                {
                    return(false);
                }

                result = this._comparer.Compare(current.Values[0], data);
            }

            if (current.Right == null)
            {
                if (parent == null)
                {
                    if (current.Left != null)
                    {
                        current.Left.Parent = parent;
                    }

                    this._root = current.Left;
                }
                else
                {
                    result = this._comparer.Compare(parent.Values[0], current.Values[0]);
                    if (result > 0)
                    {
                        if (current.Left != null)
                        {
                            current.Left.Parent = parent;
                        }

                        parent.Left = current.Left;
                    }
                    else
                    {
                        if (current.Right != null)
                        {
                            current.Right.Parent = parent;
                        }

                        parent.Right = current.Right;
                    }
                }
            }
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;
                if (current.Left != null)
                {
                    current.Left.Parent = current.Right;
                }

                if (parent == null)
                {
                    if (current.Right != null)
                    {
                        current.Right.Parent = parent;
                    }

                    this._root = current.Right;
                }
                else
                {
                    result = this._comparer.Compare(parent.Values[0], current.Values[0]);
                    if (result > 0)
                    {
                        if (current.Right != null)
                        {
                            current.Right.Parent = parent;
                        }

                        parent.Left = current.Right;
                    }
                    else
                    {
                        if (current.Right != null)
                        {
                            current.Right.Parent = parent;
                        }

                        parent.Right = current.Right;
                    }
                }
            }
            else
            {
                BinarySearchTreeNode <T> leftMost = current.Right, lmParent = current;
                while (leftMost.Left != null)
                {
                    lmParent = leftMost;
                    leftMost = lmParent.Left;
                }

                lmParent.Left  = leftMost.Right;
                leftMost.Left  = current.Left;
                leftMost.Right = current.Right;
                if (lmParent.Left != null)
                {
                    lmParent.Left.Parent = leftMost;
                }

                if (leftMost.Right != null)
                {
                    leftMost.Right.Parent = lmParent;
                }

                if (current.Left != null)
                {
                    current.Left.Parent = leftMost;
                }

                if (current.Right != null)
                {
                    current.Right.Parent = leftMost;
                }

                if (parent == null)
                {
                    leftMost.Parent = parent;
                    this._root      = leftMost;
                }
                else
                {
                    result = this._comparer.Compare(parent.Values[0], current.Values[0]);
                    if (result > 0)
                    {
                        leftMost.Parent = parent;
                        parent.Left     = leftMost;
                    }
                    else
                    {
                        leftMost.Parent = parent;
                        parent.Right    = leftMost;
                    }
                }
            }

            current.Parent = current.Left = current.Right = null;
            return(true);
        }
예제 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BinarySearchTree{T}"/> class.
 ///   Basic constructor.
 /// </summary>
 public BinarySearchTree()
 {
     this._root = null;
 }
예제 #3
0
 /// <summary>
 /// Clears nodes of the tree.
 /// </summary>
 public override void Clear()
 {
     this._root = null;
 }