Beispiel #1
0
        /// <summary>
        /// Removes the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public bool Remove(T value)
        {
            BinaryTreeNode <T> current, parent;

            current = FindWithParent(value, out parent);

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

            if (current.RightTreeNode == null)
            {
                if (parent == null)
                {
                    head = current.LeftTreeNode;
                }
                else
                {
                    int result = parent.Compare(current.Value);
                    if (result > 0)
                    {
                        parent.LeftTreeNode = current.LeftTreeNode;
                    }
                    else if (result < 0)
                    {
                        parent.RightTreeNode = current.LeftTreeNode;
                    }
                }
            }
            else if (current.RightTreeNode.LeftTreeNode == null)
            {
                current.RightTreeNode.LeftTreeNode = current.LeftTreeNode;

                if (parent == null)
                {
                    head = current.RightTreeNode;
                }
                else
                {
                    int result = parent.Compare(current.Value);
                    if (result > 0)
                    {
                        parent.LeftTreeNode = current.RightTreeNode;
                    }
                    else if (result < 0)
                    {
                        parent.RightTreeNode = current.RightTreeNode;
                    }
                }
            }
            else
            {
                BinaryTreeNode <T> leftmost       = current.RightTreeNode.LeftTreeNode;
                BinaryTreeNode <T> leftmostParent = current.RightTreeNode;

                while (leftmost.LeftTreeNode != null)
                {
                    leftmostParent = leftmost;
                    leftmost       = leftmost.LeftTreeNode;
                }

                leftmostParent.LeftTreeNode = leftmost.RightTreeNode;
                leftmost.LeftTreeNode       = current.LeftTreeNode;
                leftmost.RightTreeNode      = current.RightTreeNode;

                if (parent == null)
                {
                    head = leftmost;
                }
                else
                {
                    int result = parent.Compare(current.Value);
                    if (result > 0)
                    {
                        parent.LeftTreeNode = leftmost;
                    }
                    else if (result < 0)
                    {
                        parent.RightTreeNode = leftmost;
                    }
                }
            }

            Count--;
            Version++;

            return(true);
        }
Beispiel #2
0
 /// <summary>
 /// Clears this instance.
 /// </summary>
 public void Clear()
 {
     head  = null;
     Count = 0;
     Version++;
 }