Set (BST-based)

A set implementation that uses a binary search tree to keep track of the unique elements.

Pros Cons
Keeps track of ordering of elements Worse average times than hash implementation
If binary search tree isn't balanced, it could lead to linear times
Implementations
C++: set

std::set<int> s;

s.insert(10); // {10}
s.insert(20): // {10, 20}
s.insert(10); // {10, 20}

s.erase(10); // {20}

Java: TreeSet

Set<Integer> s = new TreeSet<>();

s.add(10); // {10}
s.add(20): // {10, 20}
s.add(10); // {10, 20}

s.remove(10); // {20}

Operations Worst Average
DeleteO(n)O(log n)
InsertO(n)O(log n)
SearchO(n)O(log n)

Similar