Пример #1
0
 private bool AddTo(Node <T> node, T value)
 {
     if (value.CompareTo(node.Value) < 0)
     {
         if (node.Left == null)
         {
             node.Left = new Node <T>(value, node, this);
         }
         else
         {
             AddTo(node.Left, value);
         }
     }
     else
     {
         if (node.Right == null)
         {
             node.Right = new Node <T>(value, node, this);
         }
         else
         {
             AddTo(node.Right, value);
         }
     }
     Count++;
     return(node.Balance());
 }
Пример #2
0
        public Node <T> balance(Node <T> node)
        {
            if (node.Balance() > 1)
            {
                if (node.rc.Balance() < 0)
                {
                    node.rc = rr(node);
                }

                node = rl(node);
            }

            if (node.Balance() < -1)
            {
                if (node.lc.Balance() > 0)
                {
                    node.lc = rl(node);
                }

                node = rr(node);
            }

            return(node);
        }
Пример #3
0
        public bool Remove(T value)
        {
            if (value == null)
            {
                return(false);
            }
            Node <T> current;

            current = Find(value);
            if (current == null)
            {
                return(false);
            }
            Node <T> treeToBalance = current.Parent;

            Count--;

            if (current.Right == null)
            {
                if (current.Parent == null)
                {
                    Head = current.Left;
                    if (Head != null)
                    {
                        Head.Parent = null;
                    }
                }
                else
                {
                    int result = current.Parent.CompareTo(current.Value);
                    if (result > 0)
                    {
                        current.Parent.Left = current.Left;
                    }
                    else if (result < 0)
                    {
                        current.Parent.Right = current.Left;
                    }
                }
            }
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;
                if (current.Parent == null)
                {
                    Head = current.Right;
                    if (Head != null)
                    {
                        Head.Parent = null;
                    }
                }
                else
                {
                    int result = current.Parent.CompareTo(current.Value);
                    if (result > 0)
                    {
                        current.Parent.Left = current.Right;
                    }
                    else if (result < 0)
                    {
                        current.Parent.Right = current.Right;
                    }
                }
            }
            else
            {
                Node <T> leftmost = current.Right.Left;
                while (leftmost.Left != null)
                {
                    leftmost = leftmost.Left;
                }
                leftmost.Parent.Left = leftmost.Right;
                leftmost.Left        = current.Left;
                leftmost.Right       = current.Right;
                if (current.Parent == null)
                {
                    Head = leftmost;
                    if (Head != null)
                    {
                        Head.Parent = null;
                    }
                }
                else
                {
                    int result = current.Parent.CompareTo(current.Value);
                    if (result > 0)
                    {
                        current.Parent.Left = leftmost;
                    }
                    else if (result < 0)
                    {
                        current.Parent.Right = leftmost;
                    }
                }
            }
            if (treeToBalance != null)
            {
                treeToBalance.Balance();
            }
            else
            {
                if (Head != null)
                {
                    Head.Balance();
                }
            }
            return(true);
        }