Class

TreeSet

TreeSet()

TreeSet is a container that stores unique elements following a specific order.

In a TreeSet, the value of an element also identifies it (the value is itself the key), and each value must be unique. The value of the elements in a TreeSet cannot be modified once in the container (the elements are immutable), but they can be inserted or removed from the container.

Container properties

Associative - Elements in associative containers are referenced by their key and not by their absolute position in the container. Ordered - The elements in the container follow a strict order at all times. All inserted elements are given a position in this order. Set - The value of an element is also the key used to identify it. Unique keys - No two elements in the container can have equivalent keys.

Constructor

# new TreeSet()

View Source tree-set.ts, line 33

Example
let set = new TreeSet();
// add few values
set.add(1);
set.add(2);
// check whether key exists
let flag = set.has(1); // << true
// print all keys
for (let key of set) {
  console.log(`key: ${key}`);
}

Members

# __t

Internal tree

View Source tree-set.ts, line 50

# compareFunc

Sets custom comparison function if key values are not of primitive types. Callback is a 3-way comparison function accepts two key values (lhs, rhs). It is expected to return +1 if the value of rhs is greater than lhs -1 if the value of rhs is less than lhs 0 if values are the same

View Source tree-set.ts, line 240

# size

Number of keys in the set.

View Source tree-set.ts, line 185

Methods

# add(key) → {void}

Adds a key to the set, unless the key already exists.

Parameters:
Name Type Description
key K

Key to be added

View Source tree-set.ts, line 576

void
Example
let set = new TreeSet();
set.add(1);

# backwards() → {IterableIterator}

ES6 reverse iterator for all keys in descending order.

View Source tree-set.ts, line 230

reverse iterator for all elements

IterableIterator
Example
let set = new TreeSet([1, 2, 3]);
for (let key of set.backwards()) {
  console.log(`key: ${key}`);
}

# begin() → {SetIterator}

Forward iterator to the first element

View Source tree-set.ts, line 643

iterator pointing at element with the smallest key

SetIterator
Example
let set = new TreeSet();
...
for (let it = set.begin(); !it.equals(set.end()); it.next()) {
  console.log(`key: ${it.key}`);
}

# clear()

Removes all key-value pairs.

View Source tree-set.ts, line 90

Example
let set = new TreeSet([1, 2, 3]);
set.clear();
console.log(set.size); // 0

# compareFunc(func)

Sets custom comparison function if key values are not of primitive types. Callback is a 3-way comparison function accepts two key values (lhs, rhs). It is expected to return +1 if the value of rhs is greater than lhs -1 if the value of rhs is less than lhs 0 if values are the same

Parameters:
Name Type Description
func compareFunctionType

View Source tree-set.ts, line 631

# delete(key) → {void}

Removes key-value pair with the specified key if such entry exists. Does nothing otherwise.

Parameters:
Name Type Description
key K

Key to be deleted

View Source tree-set.ts, line 513

void
Example
let set = new TreeSet([1, 2, 3]);
set.delete(2);
console.log(set.toString()); // {1,3}

# end() → {SetIterator}

Forward iterator to the element following the last element

View Source tree-set.ts, line 655

iterator pointing at the node after the element with the largest key

SetIterator
Example
let set = new TreeSet();
...
for (let it = set.begin(); !it.equals(set.end()); it.next()) {
  console.log(`key: ${it.key}`);
}

# entries() → {IterableIterator}

Forward ES6 iterator for all values in ascending order.

View Source tree-set.ts, line 524

forward iterator for all elements

IterableIterator
Example
let set = new TreeSet([1, 2, 3]);
for (let key of set.entries()) {
  console.log(`key: ${key}`);
}

# erase(iterator) → {void}

Removes value for the specified iterator.

Parameters:
Name Type Description
iterator SetIterator

pointing to the node to be removed

View Source tree-set.ts, line 716

void
Example
let set = new TreeSet([1,2,3]);
let it = set.find(2);
it.prev();
set.erase(it); // removes a node with key 1
console.log(set.toString()); // {2,3}

# find(key) → {SetIterator}

Finds an element with key equivalent to the specified one. If such key does not exist end() iterator is returned.

Parameters:
Name Type Description
key K

Key to search for

View Source tree-set.ts, line 669

iterator pointing at the found node

SetIterator
Example
let set = new TreeSet([1, 2, 3]);
...
let it = set.find(1);
if (!it.equals(set.end())) {
  console.log(`Found key: ${it.key}`); // 1
}

# first() → {K}

Returns first element of the container, or undefined if container is empty

View Source tree-set.ts, line 807

first element of the container, or undefined if container is empty

K
Example
let set = new TreeSet([1, 2, 3]);
let first = set.first(); // 1

# forEach(callback) → {void}

Iterates all values using a callback in ascending order. Note that ES6 specifies the order of key parameters in the callback differently from for-of loop.

Parameters:
Name Type Description
callback *

The same type of callback as used in forEach for regular JS maps

View Source tree-set.ts, line 538

void
Example
let set = new TreeSet([1, 2, 3]);
set.forEach(function(value, key, container) {
  // value is the same as key
  console.log(`key: ${key}, value: ${value}`);
});

# has(key) → {boolean}

A boolean indicator whether set contains the specified key.

Parameters:
Name Type Description
key K

a value of any type that can be compared with a key

View Source tree-set.ts, line 549

true if key exists in the container

boolean
Example
let set = new TreeSet([1, 2, 3]);
let b = set.get(3); // true
b = set.get(4); // false

# insertOrReplace(key) → {InsertionResult}

Adds key-value pair if such key does not exist in the map. Replaces value if such key exists

Parameters:
Name Type Description
key K

key value

View Source tree-set.ts, line 703

indicates whether a node was added and provides iterator to it.

Example
let set = new TreeSet();
let res = set.insertOrReplace(1);
if (res.wasAdded) {
  console.log(`Inserted ${res.iterator.key}`); // prints 1
}
res = set.insertOrReplace(1) // returns iterator to the previously added node
if (res.wasReplaced) {
  console.log(`Replaced ${res.iterator.key}`); // prints 1
}

# insertUnique(key) → {InsertionResult}

Adds a key if it doesn't exist

Parameters:
Name Type Description
key K

Key to add

View Source tree-set.ts, line 686

indicates whether a node was added and provides iterator to it.

Example
let set = new TreeSet();
let res = set.insertUnique(1);
if (res.wasAdded) {
  console.log(`Inserted ${res.iterator.key}`); // prints 1
}
res = set.insertUnique(1); // this step has no effect on the set
if (res.wasAdded) {
  console.log(`Inserted ${res.iterator.key}`); // not executed
}

# keys() → {IterableIterator}

Forward ES6 iterator for all keys in ascending order.

View Source tree-set.ts, line 566

forward iterator for all keys

IterableIterator
Example
// iterate all keys
let set = new TreeSet([1, 2, 3]);
for (let k of set.keys()) {
  console.log(k); // 1, 2, 3
}
// iterate all keys in reverse order
let set = new TreeSet([1, 2, 3]);
for (let k of set.keys().backwards()) {
  console.log(k); // 3, 2, 1
}

# last() → {K}

Returns last element of the container, or undefined if container is empty

View Source tree-set.ts, line 816

last element of the container, or undefined if container is empty

K
Example
let set = new TreeSet([1, 2, 3]);
let last = set.last(); // 3

# lowerBound(key) → {SetIterator}

Iterator pointing to the first element that is not less than specified key. If no such element is found, see end() iterator is returned.

Parameters:
Name Type Description
key K

Key to search for

View Source tree-set.ts, line 745

iterator pointing at found node

SetIterator
Example
let set = new TreeSet();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive
let from = set.lowerBound(0);
let to = set.upperBound(50);
let it = from;
while (!it.equals(to)) {
  console.log(it.key);
  it.next();
}

let set = new TreeSet();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive in reverse order
let from = new ReverseIterator(set.upperBound(50));
let to = new ReverseIterator(set.lowerBound(0));
let it = from;
while (!it.equals(to)) {
  console.log(it.key);
  it.next();
}

# rbegin() → {SetIterator}

Returns iterator to the first element for reverse iterator

View Source tree-set.ts, line 757

iterator pointing to the node with the highest key

SetIterator
Example
let set = new TreeSet();
...
for (let it = set.rbegin(); !it.equals(set.rend()); it.next()) {
  console.log(`key: ${it.key}`);
}

# rend() → {SetIterator}

Returns end iterator for reverse iteration, e.g. pointing to a position after the last element

View Source tree-set.ts, line 769

iterator pointing to the node preceding the node with the lowest key

SetIterator
Example
let set = new TreeSet();
...
for (let it = set.rbegin(); !it.equals(set.rend()); it.next()) {
  console.log(`key: ${it.key}`);
}

# size() → {number}

Number of keys in the set.

View Source tree-set.ts, line 582

count of elements in the container

number

# toString() → {string}

Serializes contents of the set in the form {key1,key2,...}

View Source tree-set.ts, line 822

string representation of the set

string

# upperBound(key) → {SetIterator}

Iterator pointing to the first element that is greater than key. If no such element is found end() iterator is returned.

Parameters:
Name Type Description
key k

Key to search for

View Source tree-set.ts, line 798

iterator pointing at found node

SetIterator
Example
let set = new TreeSet();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive
let from = set.lowerBound(0);
let to = set.upperBound(50);
let it = from;
while (!it.equals(to)) {
  console.log(it.key);
  it.next();
}

let set = new TreeSet();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive in reverse order
let from = new ReverseIterator(set.upperBound(50));
let to = new ReverseIterator(set.lowerBound(0));
let it = from;
while (!it.equals(to)) {
  console.log(it.key);
  it.next();
}

# values() → {IterableIterator}

Forward ES6 iterator for all keys in ascending order. It is the same as keys() method

View Source tree-set.ts, line 599

forward iterator for all values

IterableIterator
Example
// iterate all values
let set = new TreeSet([1, 2, 3]);
for (let v of set.values()) {
  console.log(v); // '1', '2', '3'
}
// iterate all values in reverse order
let set = new TreeSet([1, 2, 3]);
for (let v of set.values().backwards()) {
  console.log(v); // '3', '2', '1'
}