internal bool UpdateHeight(BalancedOrderStatisticTree <T> tree)
            {
                int hLeft = HeightLeft(), hRight = HeightRight();

                if (hLeft > hRight + 1)
                {
                    //Debug.Assert (LeftChild != null);
                    int hLLeft = LeftChild.HeightLeft(), hLRight = LeftChild.HeightRight();
                    if (hLLeft < hLRight)
                    {
                        LeftChild.RotateRightUp(hLLeft, tree);
                    }
                    RotateLeftUp(hRight, tree);
                    Parent.Height = 0;                     // Force height of parent (who we just rotated up there) to update
                    return(true);
                }
                else if (hRight > hLeft + 1)
                {
                    //Debug.Assert (RightChild != null);
                    int hRLeft = RightChild.HeightLeft(), hRRight = RightChild.HeightRight();
                    if (hRLeft > hRRight)
                    {
                        RightChild.RotateLeftUp(hRRight, tree);
                    }
                    RotateRightUp(hLeft, tree);
                    Parent.Height = 0;                     // Force height of parent (who we just rotated up there) to update
                    return(true);
                }

                if (hLeft > hRight)
                {
                    if (Height != hLeft + 1)
                    {
                        Height = hLeft + 1;
                        return(true);
                    }
                    return(false);
                }
                else
                {
                    if (Height != hRight + 1)
                    {
                        Height = hRight + 1;
                        return(true);
                    }
                    return(false);
                }
            }
            void RotateLeftUp(int rightHeight, BalancedOrderStatisticTree <T> tree)
            {
                //Debug.Assert (LeftChild != null);

                Node child  = LeftChild;
                int  hLeft  = child.HeightLeft();
                int  hRight = child.HeightRight();

                LeftChild        = child.RightChild;
                child.RightChild = this;

                child.Parent = this.Parent;
                this.Parent  = child;

                if (LeftChild != null)
                {
                    LeftChild.Parent = this;
                }

                if (child.Parent == null)
                {
                    tree.Root = child;
                }
                else
                {
                    if (child.Parent.LeftChild == this)
                    {
                        child.Parent.LeftChild = child;
                    }
                    else
                    {
                        //Debug.Assert (child.Parent.RightChild == this);
                        child.Parent.RightChild = child;
                    }
                }

                Left        = child.Right;
                child.Right = Left + 1 + Right;

                Height       = Math.Max(hRight, rightHeight) + 1;
                child.Height = Math.Max(hLeft, Height) + 1;
            }
            void RotateRightUp(int leftHeight, BalancedOrderStatisticTree <T> tree)
            {
                Node child = RightChild;

                //Debug.Assert (RightChild != null);
                //Debug.Assert (child.Verify ());

                int hLeft = child.HeightLeft(), hRight = child.HeightRight();

                RightChild      = child.LeftChild;
                child.LeftChild = this;

                child.Parent = this.Parent;
                this.Parent  = child;
                if (RightChild != null)
                {
                    RightChild.Parent = this;
                }
                if (child.Parent == null)
                {
                    tree.Root = child;
                }
                else
                {
                    if (child.Parent.LeftChild == this)
                    {
                        child.Parent.LeftChild = child;
                    }
                    else
                    {
                        //Debug.Assert (child.Parent.RightChild == this);
                        child.Parent.RightChild = child;
                    }
                }

                Right      = child.Left;
                child.Left = Left + 1 + Right;

                Height       = Math.Max(hLeft, leftHeight) + 1;
                child.Height = Math.Max(hRight, Height) + 1;
            }
 public void InsertLeft(BalancedOrderStatisticTree <T> tree, Node n)
 {
     Insert(tree, n, false);
 }
 public void InsertRight(BalancedOrderStatisticTree <T> tree, Node n)
 {
     Insert(tree, n, true);
 }
            void Insert(BalancedOrderStatisticTree <T> tree, Node n, bool useRight)
            {
                int cmp = tree.Comparer.Compare(n.Value, Value);

                if (cmp == 0)
                {
                    if (useRight)
                    {
                        cmp = 1;
                    }
                    else
                    {
                        cmp = -1;
                    }
                }

                if (cmp < 0)                   // n.value < this.value
                {
                    Left++;
                    if (LeftChild == null)
                    {
                        n.Parent  = this;
                        LeftChild = n;
                        if (RightChild == null)
                        {
                            Height++;
                            for (Node p = Parent; p != null; p = p.Parent)
                            {
                                if (!p.UpdateHeight(tree))
                                {
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        LeftChild.Insert(tree, n, useRight);
                    }
                }
                else                     // n.value > this.value
                {
                    Right++;
                    if (RightChild == null)
                    {
                        n.Parent   = this;
                        RightChild = n;
                        if (LeftChild == null)
                        {
                            Height++;
                            for (Node p = Parent; p != null; p = p.Parent)
                            {
                                if (!p.UpdateHeight(tree))
                                {
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        RightChild.Insert(tree, n, useRight);
                    }
                }
            }