TreeSet / Binary Search Tree
๐ Quick Reference
| Property | Value |
|---|---|
| Type | Sorted set backed by Red-Black Tree |
| Add/Remove/Contains | O(log n) |
| First/Last | O(log n) |
| Memory | O(n) with node overhead |
| Ordering | Natural order or custom Comparator |
| Best For | Sorted unique elements, range queries |
๐ฎ Interactive Visualizer
Watch how TreeSet maintains balance during insertions and deletions:
Loading visualizer...
- Insert elements - see tree balancing (rotations)
- Delete a node - watch restructuring
- Find an element - observe O(log n) path
- Try inserting duplicates - they're ignored!
๐ง Key Operations
Creating
JAVA(9 lines)CodeLoading syntax highlighter...
Adding/Removing
JAVA(7 lines)CodeLoading syntax highlighter...
Querying
JAVA(7 lines)CodeLoading syntax highlighter...
Navigation (Sorted Operations)
JAVA(15 lines)CodeLoading syntax highlighter...
Iteration
JAVA(8 lines)CodeLoading syntax highlighter...
๐ Complexity Analysis
| Operation | Time | Notes |
|---|---|---|
add(e) | O(log n) | Tree traversal + possible rebalancing |
remove(e) | O(log n) | Find + restructure |
contains(e) | O(log n) | Binary search in tree |
first/last | O(log n) | Traverse to leftmost/rightmost |
floor/ceiling | O(log n) | Modified binary search |
subSet/headSet/tailSet | O(1) | Returns view, not copy |
iteration | O(n) | In-order traversal |
Red-Black Tree Properties
1. Every node is RED or BLACK 2. Root is BLACK 3. Leaves (NIL) are BLACK 4. RED node โ both children BLACK 5. All paths from node to leaves have same BLACK count These rules ensure: height โค 2 ร log(n+1)
โ When to Use TreeSet
Good Use Cases
- Sorted unique elements - always in order
- Range queries - subSet, headSet, tailSet
- Finding neighbors - floor, ceiling, lower, higher
- Min/Max operations - first(), last()
Avoid When
- Just need uniqueness - use
HashSet(O(1) vs O(log n)) - Frequent contains checks - HashSet is faster
- Need index access - TreeSet has no get(i)
- Null elements needed - TreeSet doesn't allow null
๐ TreeSet vs HashSet vs LinkedHashSet
| Feature | HashSet | LinkedHashSet | TreeSet |
|---|---|---|---|
| Order | None | Insertion | Sorted |
| add/contains | O(1) | O(1) | O(log n) |
| Memory | Lowest | Medium | Highest |
| Null allowed | Yes | Yes | No |
| Range queries | No | No | Yes |
JAVA(4 lines)CodeLoading syntax highlighter...
โ ๏ธ Common Pitfalls
1. Inconsistent Comparator with equals
JAVA(10 lines)CodeLoading syntax highlighter...
2. Mutating Elements After Insert
JAVA(10 lines)CodeLoading syntax highlighter...
3. Using Non-Comparable Types
JAVA(6 lines)CodeLoading syntax highlighter...
4. Expecting O(1) Like HashSet
JAVA(8 lines)CodeLoading syntax highlighter...
๐ค Interview Tips
"TreeSet maintains sorted order using a Red-Black tree (O(log n) operations), while HashSet uses hashing (O(1) average). Choose TreeSet when you need sorted iteration or range queries; HashSet otherwise.
"TreeSet relies on comparisons to position elements. Comparing with null would throw NullPointerException. HashSet allows one null because it uses equals() for comparison.
"Elements are compared using compareTo() or the Comparator. If comparison returns 0, elements are considered equal and the duplicate is not added. This is why Comparator should be consistent with equals().
"floor(e) returns greatest element โค e (inclusive), lower(e) returns greatest element < e (exclusive). Similarly, ceiling is โฅ and higher is >.
๐ Series Navigation
TreeSet from @tomaszjarosz/react-visualizers