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 |
std::set<int> s;
s.insert(10); // {10}
s.insert(20): // {10, 20}
s.insert(10); // {10, 20}s.erase(10); // {20}
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 |
---|---|---|
Delete | O(n) | O(log n) |
Insert | O(n) | O(log n) |
Search | O(n) | O(log n) |