public void Insert(IComparable newValue)
 {
     if (newValue.CompareTo(Value) <= 0)
     {
         if (Left == null)
         {
             Left = new BalancedTreeNode(newValue);
         }
         else
         {
             Left.Insert(newValue);
         }
     }
     else
     {
         if (Right == null)
         {
             Right = new BalancedTreeNode(newValue);
         }
         else
         {
             Right.Insert(newValue);
         }
     }
     CheckAndBalance();
 }
        public BalancedTreeNode LeftRotate()
        {
            BalancedTreeNode newRoot = Right;

            Right        = Right.Left;
            newRoot.Left = this;
            return(newRoot);
        }
        public BalancedTreeNode RightRotate()
        {
            BalancedTreeNode newRoot = Left;

            Left          = Left.Right;
            newRoot.Right = this;
            return(newRoot);
        }
 public void Insert(IComparable Value)
 {
     if (Root == null)
     {
         Root = new BalancedTreeNode(Value);
         return;
     }
     Root.Insert(Value);
     CheckAndBalance();
 }
        public void Remove(IComparable val, BalancedTreeNode Previous)
        {
            bool wentLeft = val.CompareTo(Previous.Value) <= 0;

            if (Value.Equals(val))
            {
                if (IsLeaf())
                {
                    if (wentLeft)
                    {
                        Previous.Left = null;
                    }
                    else
                    {
                        Previous.Right = null;
                    }
                }
                else if (Left != null && Right == null)
                {
                    if (wentLeft)
                    {
                        Previous.Left = Left;
                    }
                    else
                    {
                        Previous.Right = Left;
                    }
                }
                else if (Right != null && Left == null)
                {
                    if (wentLeft)
                    {
                        Previous.Left = Right;
                    }
                    else
                    {
                        Previous.Right = Right;
                    }
                }
                else
                {
                    if (Left.Right != null)
                    {
                        IComparable temp = Left.Right.Value;
                        Left.Remove(temp, this);
                        Value = temp;
                    }
                    else
                    {
                        IComparable temp = Left.Value;
                        Left.Remove(temp, this);
                        Value = temp;
                    }
                }
            }
            else if (val.CompareTo(Value) > 0)
            {
                Right.Remove(val, this);
            }
            else
            {
                Left.Remove(val, this);
            }
            CheckAndBalance();
        }
 public BalancedTree(IComparable Value)
 {
     Root = new BalancedTreeNode(Value);
 }