Пример #1
0
        public bool Delete(T key)
        {
            var node = root;

            while (node != null)
            {
                if (key.CompareTo(node.Key) < 0)
                {
                    node = node.LeftChild;
                }
                else if (key.CompareTo(node.Key) > 0)
                {
                    node = node.RightChild;
                }
                else
                {
                    var left  = node.LeftChild;
                    var right = node.RightChild;
                    if (left == null)
                    {
                        if (right == null)
                        {
                            if (node == root)
                            {
                                root = null;
                            }
                            else
                            {
                                var parent = node.Parent;
                                if (parent.LeftChild == node)
                                {
                                    parent.LeftChild = null;
                                    DeleteBalance(parent, -1);
                                }
                                else
                                {
                                    parent.RightChild = null;
                                    DeleteBalance(parent, 1);
                                }
                            }
                        }
                        else
                        {
                            Transplant(node, right);
                            DeleteBalance(node, 0);
                        }
                    }
                    else if (right == null)
                    {
                        Transplant(node, left);
                        DeleteBalance(node, 0);
                    }
                    else
                    {
                        var successor = right;
                        if (successor.LeftChild == null)
                        {
                            var parent = node.Parent;
                            successor.Parent    = parent;
                            successor.LeftChild = left;
                            successor.Balance   = node.Balance;
                            if (left != null)
                            {
                                left.Parent = successor;
                            }
                            if (node == root)
                            {
                                root = successor;
                            }
                            else
                            {
                                if (parent.LeftChild == node)
                                {
                                    parent.LeftChild = successor;
                                }
                                else
                                {
                                    parent.RightChild = successor;
                                }
                            }
                            DeleteBalance(successor, 1);
                        }
                        else
                        {
                            while (successor.LeftChild != null)
                            {
                                successor = successor.LeftChild;
                            }
                            var parent          = node.Parent;
                            var successorParent = successor.Parent;
                            var successorRight  = successor.RightChild;
                            if (successorParent.LeftChild == successor)
                            {
                                successorParent.LeftChild = successorRight;
                            }
                            else
                            {
                                successorParent.RightChild = successorRight;
                            }
                            if (successorRight != null)
                            {
                                successorRight.Parent = successorParent;
                            }
                            successor.Parent     = parent;
                            successor.LeftChild  = left;
                            successor.Balance    = node.Balance;
                            successor.RightChild = right;
                            right.Parent         = successor;
                            if (left != null)
                            {
                                left.Parent = successor;
                            }
                            if (node == root)
                            {
                                root = successor;
                            }
                            else
                            {
                                if (parent.LeftChild == node)
                                {
                                    parent.LeftChild = successor;
                                }
                                else
                                {
                                    parent.RightChild = successor;
                                }
                            }
                            DeleteBalance(successorParent, -1);
                        }
                    }
                    return(true);
                }
            }
            return(false);
        }