Example #1
0
        private void BalanceAfterRemoving(AVLNode <TKey, TValue> node, int balance)
        {
            while (node != null)
            {
                node.Balance += balance;
                balance       = node.Balance;

                if (balance == 2)
                {
                    if (node.Left.Balance >= 0)
                    {
                        node = RotateRight(node);

                        if (node.Balance == -1)
                        {
                            return;
                        }
                    }
                    else
                    {
                        node = RotateLeftRight(node);
                    }
                }
                else if (balance == -2)
                {
                    if (node.Right.Balance <= 0)
                    {
                        node = RotateLeft(node);

                        if (node.Balance == 1)
                        {
                            return;
                        }
                    }
                    else
                    {
                        node = RotateRightLeft(node);
                    }
                }
                else if (balance != 0)
                {
                    return;
                }

                var parent = node.Parent;

                if (parent != null)
                {
                    if (parent.Left == node)
                    {
                        balance = -1;
                    }
                    else
                    {
                        balance = 1;
                    }
                }

                node = parent;
            }
        }
Example #2
0
 public void Clear()
 {
     _root = null;
     Count = 0;
 }
Example #3
0
        public bool Remove(TKey key)
        {
            var node = _root;

            while (node != null)
            {
                if (key.CompareTo(node.Key) < 0)
                {
                    node = node.Left;
                }
                else if (key.CompareTo(node.Key) > 0)
                {
                    node = node.Right;
                }
                else
                {
                    var left  = node.Left;
                    var right = node.Right;

                    if (left == null)
                    {
                        if (right == null)
                        {
                            if (node == _root)
                            {
                                _root = null;
                            }
                            else
                            {
                                var parent = node.Parent;

                                if (parent.Left == node)
                                {
                                    parent.Left = null;
                                    BalanceAfterRemoving(parent, -1);
                                }
                                else
                                {
                                    parent.Right = null;
                                    BalanceAfterRemoving(parent, 1);
                                }
                            }
                        }
                        else
                        {
                            Replace(node, right);
                            BalanceAfterRemoving(node, 0);
                        }
                    }
                    else if (right == null)
                    {
                        Replace(node, left);
                        BalanceAfterRemoving(node, 0);
                    }
                    else
                    {
                        var next = right;

                        if (next.Left == null)
                        {
                            var parent = node.Parent;

                            next.Parent  = parent;
                            next.Left    = left;
                            next.Balance = node.Balance;
                            left.Parent  = next;

                            if (node == _root)
                            {
                                _root = next;
                            }
                            else
                            {
                                if (parent.Left == node)
                                {
                                    parent.Left = next;
                                }
                                else
                                {
                                    parent.Right = next;
                                }
                            }

                            BalanceAfterRemoving(next, 1);
                        }
                        else
                        {
                            while (next.Left != null)
                            {
                                next = next.Left;
                            }

                            var parent     = node.Parent;
                            var nextParent = next.Parent;
                            var nextRight  = next.Right;

                            if (nextParent.Left == next)
                            {
                                nextParent.Left = nextRight;
                            }
                            else
                            {
                                nextParent.Right = nextRight;
                            }

                            if (nextRight != null)
                            {
                                nextRight.Parent = nextParent;
                            }

                            next.Parent  = parent;
                            next.Left    = left;
                            next.Balance = node.Balance;
                            next.Right   = right;
                            right.Parent = next;
                            left.Parent  = next;

                            if (node == _root)
                            {
                                _root = next;
                            }
                            else
                            {
                                if (parent.Left == node)
                                {
                                    parent.Left = next;
                                }
                                else
                                {
                                    parent.Right = next;
                                }
                            }

                            BalanceAfterRemoving(nextParent, -1);
                        }
                    }

                    Count--;

                    return(true);
                }
            }

            return(false);
        }