An ordered Set backed by a red-black tree.
Create a TreeSet from an iterable of keys or raw elements.
Iterable of keys, or raw elements if toElementFn is provided.
toElementFn
Configuration options including optional toElementFn to transform raw elements.
When using the default comparator and encountering unsupported key types, or invalid keys (e.g. NaN, invalid Date).
NaN
Date
// Standard usage with keysconst set = new TreeSet([3, 1, 2]);// Using toElementFn to transform raw objectsconst users = [{ id: 3, name: 'Alice' }, { id: 1, name: 'Bob' }];const set = new TreeSet<number, User>(users, { toElementFn: u => u.id }); Copy
// Standard usage with keysconst set = new TreeSet([3, 1, 2]);// Using toElementFn to transform raw objectsconst users = [{ id: 3, name: 'Alice' }, { id: 1, name: 'Bob' }];const set = new TreeSet<number, User>(users, { toElementFn: u => u.id });
Number of elements in the set.
Add a key to the set (no-op if already present).
Expected time O(log n)
Smallest key that is >= the given key.
Remove all keys.
Creates a shallow clone of this set.
Time O(n log n), Space O(n)
const original = new TreeSet([1, 2, 3]);const copy = original.clone();copy.add(4);original.has(4); // false (original unchanged) Copy
const original = new TreeSet([1, 2, 3]);const copy = original.clone();copy.add(4);original.has(4); // false (original unchanged)
Delete a key.
true if the key existed; otherwise false.
true
false
Iterate over [value, value] pairs (native Set convention).
[value, value]
Note: TreeSet stores only keys internally; [k, k] is created on-the-fly during iteration.
[k, k]
Test whether all values satisfy a predicate.
Optional
Time O(n), Space O(1)
Create a new TreeSet containing only values that satisfy the predicate.
Time O(n log n) expected, Space O(n)
Find the first value that satisfies a predicate.
Smallest key in the set.
Largest key that is <= the given key.
Visit each value in ascending order.
Callback follows native Set convention: (value, value2, set).
(value, value2, set)
Test whether a key exists.
Smallest key that is > the given key.
Whether the set is empty.
Iterate over keys in ascending order.
Largest key in the set.
Largest key that is < the given key.
Create a new TreeSet by mapping each value to a new key.
This mirrors RedBlackTree.map: mapping produces a new ordered container.
RedBlackTree.map
Remove and return the smallest key.
Remove and return the largest key.
Print a human-friendly representation.
Time O(n), Space O(n)
Return all keys in a given range.
[low, high]
Inclusive/exclusive bounds (defaults to inclusive).
Reduce values into a single accumulator.
Test whether any value satisfies a predicate.
Materialize the set into an array of keys.
Iterate over values in ascending order.
Note: for Set-like containers, values() is the same as keys().
values()
keys()
Static
Create the strict default comparator.
Supports:
number
-0
0
string
getTime()
For other key types, a custom comparator must be provided.
An ordered Set backed by a red-black tree.