javascript-collections-framework
    Preparing search index...

    Class TreeSet<E>Sealed

    A set kept in sorted order, stored as a balanced binary search tree: add, has, delete and the nearest-match queries lower, floor, ceiling and higher cost O(log n). The order is the natural order of the members, or the comparator option.

    Membership is decided by the order alone: two values are the same member when the comparator returns 0 for them, whatever their identity. Iteration, toArray, toJSON and toString visit the members in ascending order. subSet, headSet and tailSet return live views of a range, and reversed() a live view in descending order; changes through any view reach the set.

    In natural order, null and undefined cannot be added, and has and delete answer false for them and for values of another kind, as a native Set does; -0 is stored as 0. A comparator that throws or returns NaN leaves the set as it was. A comparator that is not a consistent total order may make lookups give wrong answers, but never loses members or loops. Adding or removing members while iterating throws ConcurrentModificationError; use a cursor to remove while iterating.

    Two sets are equal when they have the same size and each has every member of the other. The class is not designed to be subclassed.

    const times = new TreeSet([1030, 900, 1415])
    times.ceiling(1000) // 1030: the next slot at or after 10:00
    times.headSet(1030).toArray() // [900]

    const byLength = new TreeSet<string>({ comparator: (a, b) => a.length - b.length })
    byLength.add('bb').add('a').add('cc') // 'cc' is the same member as 'bb', so it is not added
    byLength.toArray() // ['a', 'bb']

    Type Parameters

    • E

    Implements

    Index
    • Creates a set in natural order holding the distinct elements of elements; empty when elements is omitted, undefined or null.

      Time: O(m) with at most m − 1 comparisons (one when m is 1 in natural order) for m elements in strictly ascending order; O(m log m) otherwise.

      Type Parameters

      • E

      Parameters

      • Optionalelements: Iterable<E, any, any> | null

        The initial elements; of several equal ones the first is kept. The comparator of a TreeSet given here is not used; pass it as an option, or call copy().

      Returns TreeSet<E>

      TypeError when elements is neither iterable nor null/undefined.

      NullElementError when an element is null or undefined.

      ClassCastError when the elements cannot be compared in natural order.

      new TreeSet([3, 1, 2, 1]) // TreeSet(3) {1, 2, 3}
      
    • Creates an empty set with the given options.

      Time: O(1).

      Type Parameters

      • E

      Parameters

      Returns TreeSet<E>

      TypeError when comparator is neither undefined nor a function.

      new TreeSet<string>({ comparator: (a, b) => b.localeCompare(a) })
      
    • Creates a set holding the distinct elements of elements (empty when undefined or null), ordered by options.comparator, or by natural order when it is omitted.

      Time: O(m) with at most m − 1 comparisons (one when m is 1 in natural order) for m elements in strictly ascending order; O(m log m) otherwise.

      Type Parameters

      • E

      Parameters

      • elements: Iterable<E, any, any> | null | undefined

        The initial elements; of several equal ones the first is kept.

      • Optionaloptions: TreeSetOptions<E>

        The options; comparator is read once.

      Returns TreeSet<E>

      TypeError when elements is neither iterable nor null/undefined, when options is not an object, or when comparator is neither undefined nor a function, or returns a non-number or NaN.

      NullElementError when, in natural order, an element is null or undefined.

      ClassCastError when, in natural order, the elements cannot be compared.

      new TreeSet([1, 3, 2], { comparator: (a, b) => b - a }) // TreeSet(3) {3, 2, 1}
      
    • get "[COLLECTION_BRAND]"(): true

      Marks collections so isCollection recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[SEQUENCED_COLLECTION_BRAND]"(): true

      Marks sequenced collections so isSequencedCollection recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[SET_COLLECTION_BRAND]"(): true

      Marks sets so isSetCollection recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[SORTED_SET_BRAND]"(): true

      Marks sorted sets so isSortedSet recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[toStringTag]"(): string

      'TreeSet', for Object.prototype.toString. Time: O(1).

      Returns string

    • get "[TREE_SET_BRAND]"(): true

      Marks instances of this class so instanceof recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get revision(): number

      A number that changes on every change to the members and never on a read or on a call that changes nothing. Its exact values are unspecified; views share it. Time: O(1).

      Returns number

    • get size(): number

      The number of members. Time: O(1).

      Returns number

    • The value-equality protocol; delegates to equals. Time: as equals.

      Parameters

      • other: unknown

      Returns boolean

    • The value-equality protocol: an order-independent combination of the members' hashes through hash. Equal sets get the same value when their comparators treat as equal only values hash hashes alike, as natural order does for primitives. Values are unspecified. Time: O(n) plus the hash calls.

      Parameters

      Returns number

    • Node's inspect hook: TreeSet(2) { 1, 2 } in console.log. Time: as toString.

      Parameters

      • depth: number | null
      • options: object
      • inspect: (value: unknown, options: object) => string

      Returns string

    • Returns an iterator over the members in ascending order.

      Time: O(log n) to create; O(1) amortised per step.

      Returns IterableIterator<E>

      A new iterator.

      ConcurrentModificationError (from next()) when members are added or removed after the iterator is created, including through a cursor.

      for (const n of new TreeSet([2, 1])) console.log(n) // 1, then 2
      
    • Adds element unless a member compares equal to it (that member is kept), and returns the set, so calls chain. On an error nothing is added.

      Time: O(log n), with at most 2⌈log2(n + 1)⌉ + 2 comparisons.

      Parameters

      • element: E

        The element to add (in natural order, -0 is stored as 0).

      Returns this

      The set.

      NullElementError when, in natural order, element is null or undefined.

      ClassCastError when, in natural order, element cannot be compared with the members.

      TypeError when the comparator returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes members; the set keeps those changes and adds nothing itself.

      new TreeSet<number>().add(2).add(1).add(2) // TreeSet(2) {1, 2}
      
    • Adds every element of elements, in order, after reading them all and, in natural order, checking that none is null or undefined (if one is, nothing is added). If a comparison fails part-way, the elements added before it stay.

      Time: O(m log(n + m)) for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to add.

      Returns boolean

      true when any element was added.

      TypeError when elements is not iterable, or the comparator returns a non-number or NaN.

      NullElementError when, in natural order, an element is null or undefined.

      ClassCastError when, in natural order, an element cannot be compared.

      ConcurrentModificationError when the comparator adds or removes members.

      new TreeSet([1]).addAll([1, 2]) // true
      
    • Always throws: a sorted set places each member by its order. Use add.

      Time: O(1).

      Parameters

      • element: E

        Ignored.

      Returns this

      Never returns.

      UnsupportedOperationError always.

      new TreeSet([2]).addFirst(1) // throws UnsupportedOperationError
      
    • Always throws: a sorted set places each member by its order. Use add.

      Time: O(1).

      Parameters

      • element: E

        Ignored.

      Returns this

      Never returns.

      UnsupportedOperationError always.

      new TreeSet([2]).addLast(3) // throws UnsupportedOperationError
      
    • Returns the least member greater than or equal to element, or undefined when there is none.

      Time: O(log n), with at most 2⌈log2(n + 1)⌉ comparisons.

      Parameters

      • element: E

        The value to search from; it need not be a member.

      Returns E | undefined

      The member, or undefined.

      NullElementError when, in natural order, element is null or undefined.

      ClassCastError when, in natural order, element cannot be compared with the members.

      TypeError when the comparator returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes members.

      new TreeSet([1, 3, 5]).ceiling(4) // 5
      
    • Removes every member.

      Time: O(1).

      Returns void

      set.clear() // set.size === 0
      
    • Returns the comparator given at construction, or undefined when the set uses natural order.

      Time: O(1).

      Returns Comparator<E> | undefined

      The comparator, or undefined.

      const byLength = (a: string, b: string) => a.length - b.length
      new TreeSet({ comparator: byLength }).comparator() === byLength // true
      new TreeSet().comparator() // undefined
    • Returns a new TreeSet with the same members and the same comparator, independent of this one (the members themselves are not copied). The comparator is not called.

      Time: O(n).

      Returns TreeSet<E>

      The copy.

      const copy = set.copy()
      copy.add(9) // set is unchanged
    • Returns a cursor over the members in ascending order; remove() removes the member next() returned without calling the comparator. For descending order, use reversed().cursor().

      Time: O(log n) to create; next O(1) amortised; remove O(log n).

      Returns Cursor<E>

      A new cursor.

      const cursor = set.cursor()
      while (cursor.hasNext()) if (cursor.next() < 0) cursor.remove()
    • Removes the member that compares equal to element. In natural order, null, undefined and values that cannot be compared with the members remove nothing.

      Time: O(log n), with at most 2⌈log2(n + 1)⌉ comparisons.

      Parameters

      • element: E

        The value to remove.

      Returns boolean

      true when a member was removed.

      TypeError when the comparator or a [compareSymbol] method returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes members; the set removes nothing itself.

      new TreeSet([1, 2]).delete(1) // true
      
    • Returns an iterator over [member, member] pairs in ascending order, as native Set does.

      Time: O(log n) to create; O(1) amortised per step.

      Returns IterableIterator<[E, E]>

      A new iterator.

      ConcurrentModificationError (from next()) when members are added or removed after the iterator is created.

      [...new TreeSet(['a']).entries()] // [['a', 'a']]
      
    • Returns whether other is a set of the same size in which every member of this set is present, and which has every member of this set — so a TreeSet equals a HashSet with the same members, and the answer is the same both ways round. Lists, queues, arrays and native Set are never equal to it.

      Time: O(n log n), plus the has calls on other.

      Parameters

      • other: unknown

        Any value.

      Returns boolean

      true when other is an equal set.

      new TreeSet([1, 2]).equals(new HashSet([2, 1])) // true
      
    • Returns the greatest member less than or equal to element, or undefined when there is none.

      Time: O(log n), with at most 2⌈log2(n + 1)⌉ comparisons.

      Parameters

      • element: E

        The value to search from; it need not be a member.

      Returns E | undefined

      The member, or undefined.

      NullElementError when, in natural order, element is null or undefined.

      ClassCastError when, in natural order, element cannot be compared with the members.

      TypeError when the comparator returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes members.

      new TreeSet([1, 3, 5]).floor(4) // 3
      
    • Calls action(member, member, set) for each member in ascending order, with this set to thisArg, as native Set does.

      Time: O(n) plus the calls.

      Parameters

      • action: (element: E, same: E, set: SetCollection<E>) => void

        Called with each member twice and the set.

      • OptionalthisArg: unknown

        The this value for action.

      Returns void

      TypeError when action is not a function.

      ConcurrentModificationError when action adds or removes members.

      new TreeSet(['b', 'a']).forEach((tag) => console.log(tag)) // a, then b
      
    • Returns the least member. The comparator is not called.

      Time: O(log n).

      Returns E

      The least member.

      NoSuchElementError when the set is empty.

      new TreeSet([2, 1]).getFirst() // 1
      
    • Returns the greatest member. The comparator is not called.

      Time: O(log n).

      Returns E

      The greatest member.

      NoSuchElementError when the set is empty.

      new TreeSet([2, 1]).getLast() // 2
      
    • Returns whether a member compares equal to element. In natural order, null, undefined and values that cannot be compared with the members give false.

      Time: O(log n), with at most 2⌈log2(n + 1)⌉ comparisons.

      Parameters

      • element: E

        The value to look for.

      Returns boolean

      true when present.

      TypeError when the comparator or a [compareSymbol] method returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes members.

      new TreeSet([1, 2]).has(2)   // true
      new TreeSet([1, 2]).has('2') // false
    • Returns a live view of the members less than to (or equal, with inclusive: true). Changes through the view reach this set.

      Time: O(log n) to create; as subSet for the view's operations.

      Parameters

      • to: E

        The high end of the range.

      • Optionaloptions: BoundOptions | null

        inclusive (default false); read once; undefined or null for the default.

      Returns SortedSet<E>

      The view, tagged TreeSubSet.

      TypeError when options is not an object, undefined or null, or inclusive is neither undefined nor a boolean.

      NullElementError when, in natural order, to is null or undefined.

      ClassCastError when, in natural order, to cannot be compared with the members.

      new TreeSet([1, 2, 3]).headSet(3).toArray() // [1, 2]
      
    • Returns the least member greater than element, or undefined when there is none.

      Time: O(log n), with at most 2⌈log2(n + 1)⌉ comparisons.

      Parameters

      • element: E

        The value to search from; it need not be a member.

      Returns E | undefined

      The member, or undefined.

      NullElementError when, in natural order, element is null or undefined.

      ClassCastError when, in natural order, element cannot be compared with the members.

      TypeError when the comparator returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes members.

      new TreeSet([1, 3, 5]).higher(3) // 5
      
    • Returns whether the set has no members.

      Time: O(1).

      Returns boolean

      size === 0.

      new TreeSet().isEmpty() // true
      
    • Returns an iterator over the members in ascending order, as values() does.

      Time: O(log n) to create; O(1) amortised per step.

      Returns IterableIterator<E>

      A new iterator.

      ConcurrentModificationError (from next()) when members are added or removed after the iterator is created, other than through a cursor of its own.

      [...new TreeSet(['b', 'a']).keys()] // ['a', 'b']
      
    • Returns the greatest member less than element, or undefined when there is none.

      Time: O(log n), with at most 2⌈log2(n + 1)⌉ comparisons.

      Parameters

      • element: E

        The value to search from; it need not be a member.

      Returns E | undefined

      The member, or undefined.

      NullElementError when, in natural order, element is null or undefined.

      ClassCastError when, in natural order, element cannot be compared with the members.

      TypeError when the comparator returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes members.

      new TreeSet([1, 3, 5]).lower(3) // 1
      
    • Removes and returns the least member, or returns undefined when the set is empty. The comparator is not called.

      Time: O(log n).

      Returns E | undefined

      The removed member, or undefined.

      new TreeSet([2, 1]).pollFirst() // 1
      new TreeSet().pollFirst() // undefined
    • Removes and returns the greatest member, or returns undefined when the set is empty. The comparator is not called.

      Time: O(log n).

      Returns E | undefined

      The removed member, or undefined.

      new TreeSet([2, 1]).pollLast() // 2
      
    • Removes every member that compares equal to an element of elements. The elements are read and each is looked up first; an error while looking up removes nothing.

      Time: O((n + m) log n) for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to remove.

      Returns boolean

      true when any member was removed.

      TypeError when elements is not iterable, or the comparator returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes members.

      new TreeSet([1, 2, 3]).removeAll([2, 9]) // true; TreeSet(2) {1, 3}
      
    • Removes and returns the least member. The comparator is not called.

      Time: O(log n).

      Returns E

      The removed member.

      NoSuchElementError when the set is empty.

      new TreeSet([2, 1]).removeFirst() // 1
      
    • Removes every member for which predicate returns a truthy value. Every member is decided first; if predicate throws, or adds or removes members, nothing is removed.

      Time: O(n log n) plus the calls.

      Parameters

      • predicate: (element: E) => unknown

        Called with each member, in order.

      Returns boolean

      true when any member was removed.

      TypeError when predicate is not a function.

      ConcurrentModificationError when predicate adds or removes members.

      new TreeSet([1, 2, 3, 4]).removeIf((n) => n % 2 === 0) // true; TreeSet(2) {1, 3}
      
    • Removes and returns the greatest member. The comparator is not called.

      Time: O(log n).

      Returns E

      The removed member.

      NoSuchElementError when the set is empty.

      new TreeSet([2, 1]).removeLast() // 2
      
    • Removes every member that compares equal to no element of elements, as removeAll decides.

      Time: O((n + m) log n) for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to keep.

      Returns boolean

      true when any member was removed.

      TypeError when elements is not iterable, or the comparator returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes members.

      new TreeSet([1, 2, 3]).retainAll([2]) // true; TreeSet(1) {2}
      
    • Returns a live view of this set in descending order: its lower is this set's higher, its first member this set's last, and its ranges run from greater to lesser. Reversing the view returns this set.

      Time: O(1).

      Returns SortedSet<E>

      The view, tagged ReversedTreeSet.

      const set = new TreeSet([1, 2, 3])
      set.reversed().toArray() // [3, 2, 1]
      set.reversed().reversed() === set // true
    • Returns a live view of the members from from to to: from included and to excluded unless the options say otherwise. Changes through the view reach this set; adding a value outside the range through the view throws IllegalArgumentError. The view's size counts its members.

      Time: O(log n) to create; the view's operations cost O(log n), its size O(k + log n) for k members in range.

      Parameters

      • from: E

        The low end of the range.

      • to: E

        The high end of the range.

      • Optionaloptions: SubSetOptions | null

        fromInclusive (default true) and toInclusive (default false); read once; undefined or null for the defaults.

      Returns SortedSet<E>

      The view, tagged TreeSubSet.

      TypeError when options is not an object, undefined or null, or a flag is neither undefined nor a boolean.

      IllegalArgumentError when from is after to.

      NullElementError when, in natural order, a bound is null or undefined.

      ClassCastError when, in natural order, a bound cannot be compared with the members.

      const set = new TreeSet([1, 2, 3, 4, 5])
      set.subSet(2, 4).toArray() // [2, 3]
      set.subSet(2, 4, { toInclusive: true }).toArray() // [2, 3, 4]
    • Returns a live view of the members greater than or equal to from (or greater only, with inclusive: false). Changes through the view reach this set.

      Time: O(log n) to create; as subSet for the view's operations.

      Parameters

      • from: E

        The low end of the range.

      • Optionaloptions: BoundOptions | null

        inclusive (default true); read once; undefined or null for the default.

      Returns SortedSet<E>

      The view, tagged TreeSubSet.

      TypeError when options is not an object, undefined or null, or inclusive is neither undefined nor a boolean.

      NullElementError when, in natural order, from is null or undefined.

      ClassCastError when, in natural order, from cannot be compared with the members.

      new TreeSet([1, 2, 3]).tailSet(2).toArray() // [2, 3]
      
    • Returns a new array of the members in ascending order.

      Time: O(n).

      Returns E[]

      A new array.

      new TreeSet([2, 1]).toArray() // [1, 2]
      
    • Returns a new array of the members in ascending order, so JSON.stringify writes the set as an array.

      Time: O(n).

      Returns E[]

      A new array.

      JSON.stringify(new TreeSet([2, 1])) // '[1,2]'
      
    • Returns the class name, size and members in ascending order, e.g. TreeSet(2) {1, 2}.

      Time: O(length of the output) for flat contents.

      Returns string

      The text.

      String(new TreeSet([2, 1])) // 'TreeSet(2) {1, 2}'
      
    • Returns an iterator over the members in ascending order.

      Time: O(log n) to create; O(1) amortised per step.

      Returns IterableIterator<E>

      A new iterator.

      ConcurrentModificationError (from next()) when members are added or removed after the iterator is created.

      [...new TreeSet(['b', 'a']).values()] // ['a', 'b']
      
    • Returns whether value is a TreeSet, including one created by another copy of the library with the same major version. Its range and reversed views are not.

      Time: O(1).

      Parameters

      • value: unknown

        The value on the left of instanceof.

      Returns boolean

      true when value is a TreeSet.

      new TreeSet() instanceof TreeSet // true
      
    • Returns a new set of the distinct elements of elements, ordered by options.comparator, or by natural order when it is omitted. The comparator of a TreeSet given as elements is not used.

      Time: O(m) with at most m − 1 comparisons (one when m is 1 in natural order) for m elements in strictly ascending order; O(m log m) otherwise.

      Type Parameters

      • T

      Parameters

      • elements: Iterable<T>

        The elements; of several equal ones the first is kept.

      • Optionaloptions: TreeSetOptions<T>

        The options; comparator is read once.

      Returns TreeSet<T>

      A new set.

      TypeError when elements is not iterable (null and undefined included), when options is given and is not an object, or when comparator is neither undefined nor a function, or returns a non-number or NaN.

      NullElementError when, in natural order, an element is null or undefined.

      ClassCastError when, in natural order, the elements cannot be compared.

      TreeSet.from(new Set(['b', 'a'])).getFirst() // 'a'