internal virtual AvlTree Add(ElaValue key, ElaValue value) { var result = default(AvlTree); if (key.CompareTo(Key) > 0) result = new AvlTree(Key, Value, Left, Right.Add(key, value)); else result = new AvlTree(Key, Value, Left.Add(key, value), Right); return MakeBalanced(result); }
internal virtual AvlTree Remove(ElaValue key) { var result = default(AvlTree); var compare = key.CompareTo(Key); if (compare == 0) { if (Right.IsEmpty && Left.IsEmpty) result = Empty; else if (Right.IsEmpty && !Left.IsEmpty) result = Left; else if (!Right.IsEmpty && Left.IsEmpty) result = Right; else { var successor = Right; while (!successor.Left.IsEmpty) successor = successor.Left; result = new AvlTree(successor.Key, successor.Value, Left, Right.Remove(successor.Key)); } } else if (compare < 0) result = new AvlTree(Key, Value, Left.Remove(key), Right); else result = new AvlTree(Key, Value, Left, Right.Remove(key)); return MakeBalanced(result); }
internal virtual AvlTree Search(ElaValue key) { var compare = key.CompareTo(Key); if (compare == 0) return this; else if (compare > 0) return Right.Search(key); else return Left.Search(key); }