private void RotateLeft() { if (Right == null) { throw new InvalidOperationException(); } var oldLeft = Left; var oldRight = Right; Right = Right.Right; Left = new RandomizedTree <T>(Value); Value = oldRight.Value; Left.Right = oldRight.Left; Left.Left = oldLeft; }
private static IEnumerable <T> GetTreeValues(RandomizedTree <T> node) { if (node.Left != null) { foreach (var value in GetTreeValues(node.Left)) { yield return(value); } } yield return(node.Value); if (node.Right != null) { foreach (var value in GetTreeValues(node.Right)) { yield return(value); } } }
private void Insert(T key, bool insertToRoot) { Count++; if (!hasValue) { Value = key; hasValue = true; return; } if (!insertToRoot) { insertToRoot = ShouldInsertToRoot(); } if (key.CompareTo(Value) < 0) { if (Left == null) { Left = new RandomizedTree <T>(); } Left.Insert(key, insertToRoot); if (insertToRoot) { RotateRight(); } } else { if (Right == null) { Right = new RandomizedTree <T>(); } Right.Insert(key, insertToRoot); if (insertToRoot) { RotateLeft(); } } }