private static RtNode <T> Find(RtNode <T> rtNode, T key)
 {
     while (true)
     {
         if (rtNode == null)
         {
             return(null);
         }
         if (rtNode.Key.CompareTo(key) == 0)
         {
             return(rtNode);
         }
         rtNode = key.CompareTo(rtNode.Key) < 0 ? rtNode.Left : rtNode.Right;
     }
 }
        private static RtNode <T> RotateLeft(RtNode <T> rtNode)
        {
            var nodeRight = rtNode.Right;

            if (nodeRight == null)
            {
                return(rtNode);
            }
            rtNode.Right   = nodeRight.Left;
            nodeRight.Left = rtNode;
            nodeRight.Size = rtNode.Size;
            FixSize(rtNode);
            FixSize(nodeRight);
            return(nodeRight);
        }
        private static RtNode <T> InsertAsRoot(RtNode <T> rtNode, T key)
        {
            if (rtNode == null)
            {
                return(new RtNode <T>(key));
            }
            if (key.CompareTo(rtNode.Key) < 0)
            {
                rtNode.Left = InsertAsRoot(rtNode.Left, key);
                return(RotateRight(rtNode));
            }

            if (key.CompareTo(rtNode.Key) > 0)
            {
                rtNode.Right = InsertAsRoot(rtNode.Right, key);
                return(RotateLeft(rtNode));
            }

            return(Insert(rtNode, key));
        }
 private static RtNode <T> Insert(RtNode <T> rtNode, T key)
 {
     if (rtNode == null)
     {
         return(new RtNode <T>(key));
     }
     if (_randomizer.Next(rtNode.Size + 1) == 0)
     {
         return(InsertAsRoot(rtNode, key));
     }
     if (key.CompareTo(rtNode.Key) < 0)
     {
         rtNode.Left = Insert(rtNode.Left, key);
     }
     else
     {
         rtNode.Right = Insert(rtNode.Right, key);
     }
     FixSize(rtNode);
     return(rtNode);
 }
        private static RtNode <T> Join(RtNode <T> first, RtNode <T> second) // объединение двух деревьев
        {
            if (first == null)
            {
                return(second);
            }
            if (second == null)
            {
                return(first);
            }

            if (_randomizer.Next(first.Size + second.Size) < first.Size)
            {
                first.Right = Join(first.Right, second);
                FixSize(first);
                return(first);
            }

            second.Left = Join(first, second.Left);
            FixSize(second);
            return(second);
        }
        private static RtNode <T> Remove(RtNode <T> rtNode, T key)
        {
            if (rtNode == null)
            {
                return(rtNode);
            }

            if (key.CompareTo(rtNode.Key) == 0)
            {
                var node = Join(rtNode.Left, rtNode.Right);
                return(node);
            }

            if (key.CompareTo(rtNode.Key) < 0)
            {
                rtNode.Left = Remove(rtNode.Left, key);
            }
            else
            {
                rtNode.Right = Remove(rtNode.Right, key);
            }
            FixSize(rtNode);
            return(rtNode);
        }
 private static void FixSize(RtNode <T> rtNode)
 {
     rtNode.Size = GetSize(rtNode.Left) + GetSize(rtNode.Right) + 1;
 }
 private static int GetSize(RtNode <T> rtNode)
 {
     return(rtNode == null ? 0 : rtNode.Size);
 }
 public override void Remove(T key)
 {
     _root = Remove(_root, key);
 }
 public override void Insert(T key)
 {
     _root = Insert(_root, key);
 }