Class TreeSet<K, R>

An ordered Set backed by a red-black tree.

  • Iteration order is ascending by key.
  • No node exposure: all APIs use keys only.

Type Parameters

  • K = any
  • R = K

Implements

  • Iterable<K>

Constructors

  • Create a TreeSet from an iterable of keys or raw elements.

    Type Parameters

    • K = any
    • R = K

    Parameters

    • elements: Iterable<K, any, any> | Iterable<R, any, any> = []

      Iterable of keys, or raw elements if toElementFn is provided.

    • options: TreeSetOptions<K, R> = {}

      Configuration options including optional toElementFn to transform raw elements.

    Returns TreeSet<K, R>

    When using the default comparator and encountering unsupported key types, or invalid keys (e.g. NaN, invalid Date).

    // Standard usage with keys
    const set = new TreeSet([3, 1, 2]);

    // Using toElementFn to transform raw objects
    const users = [{ id: 3, name: 'Alice' }, { id: 1, name: 'Bob' }];
    const set = new TreeSet<number, User>(users, { toElementFn: u => u.id });

Accessors

Methods

  • Iterate over [value, value] pairs (native Set convention).

    Note: TreeSet stores only keys internally; [k, k] is created on-the-fly during iteration.

    Returns IterableIterator<[K, K], any, any>

  • Visit each value in ascending order.

    Callback follows native Set convention: (value, value2, set).

    Parameters

    • cb: ((value: K, value2: K, set: TreeSet<K, K>) => void)
        • (value, value2, set): void
        • Parameters

          Returns void

    • OptionalthisArg: any

    Returns void

  • Return all keys in a given range.

    Parameters

    • range: [K, K]

      [low, high]

    • options: TreeSetRangeOptions = {}

      Inclusive/exclusive bounds (defaults to inclusive).

    Returns K[]

  • Create the strict default comparator.

    Supports:

    • number (rejects NaN; treats -0 and 0 as equal)
    • string
    • Date (orders by getTime(), rejects invalid dates)

    For other key types, a custom comparator must be provided.

    Type Parameters

    • K

    Returns Comparator<K>