Home Reference Source
import {TreeSet} from 'jstreemap/src/public/tree-set.js'
public class | source

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.

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}`);
}

Static Member Summary

Static Public Members
public static get

Allows to create programmatically an instance of the same class

Constructor Summary

Public Constructor
public

constructor(iterable: *)

Creates an empty, or a pre-initialized set.

Member Summary

Public Members
public get

String tag of this class

public set

Sets custom comparison function if key values are not of primitive types.

public get

size: Number: *

Number of keys in the set.

Method Summary

Public Methods
public

Forward ES6 iterator for all keys in ascending order.

public

add(key: *)

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

public

ES6 reverse iterator for all keys in descending order.

public

Forward iterator to the first element

public

clear()

Removes all key-value pairs.

public

delete(key: *)

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

public

Forward iterator to the element following the last element

public

Forward ES6 iterator for all values in ascending order.

public

erase(iterator: Iterator)

Removes value for the specified iterator.

public

find(key: *): Iterator

Finds an element with key equivalent to the specified one.

public

first(): *

public

forEach(callback: *)

Iterates all values using a callback in ascending order.

public

has(key: *): Boolean

A boolean indicator whether set contains the specified key.

public

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

public

Adds a key if it doesn't exist

public

Forward ES6 iterator for all keys in ascending order.

public

last(): *

public

lowerBound(key: *): Iterator

Iterator pointing to the first element that is not less than specified key.

public

Reverse iterator to the last element.

public

Reverse iterator pointing to before the first element.

public

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

public

upperBound(key: *): Iterator

Iterator pointing to the first element that is greater than key.

public

values(): JsITerator

Forward ES6 iterator for all keys in ascending order.

Static Public Members

public static get [Symbol.species]: *: * source

Allows to create programmatically an instance of the same class

Return:

*

constructor object for this class.

Example:

let set = new TreeSet();
let constrFunc = Object.getPrototypeOf(set).constructor[Symbol.species];
let set2 = new constrFunc();

Public Constructors

public constructor(iterable: *) source

Creates an empty, or a pre-initialized set.

Params:

NameTypeAttributeDescription
iterable *
  • optional

Another iterable object whose values are added into the newly created set.

Example:

// Create an empty set
let set = new TreeSet();
// Create and initialize set
let set2 = new TreeSet([1, 2, 3]);

Public Members

public get [Symbol.toStringTag]: String: string source

String tag of this class

Return:

String

Example:

Object.prototype.toString.call(new TreeSet()); // "[object TreeSet]"

public set compareFunc source

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

public get size: Number: * source

Number of keys in the set.

Return:

Number

Public Methods

public [Symbol.iterator](): JsIterator source

Forward ES6 iterator for all keys in ascending order. The same as entries() method

Return:

JsIterator

Example:

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

public add(key: *) source

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

Params:

NameTypeAttributeDescription
key *

Example:

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

public backward(): JsReverseIterator source

ES6 reverse iterator for all keys in descending order.

Example:

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

public begin(): Iterator source

Forward iterator to the first element

Return:

Iterator

Example:

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

public clear() source

Removes all key-value pairs.

Example:

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

public delete(key: *) source

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

Params:

NameTypeAttributeDescription
key *

Example:

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

public end(): Iterator source

Forward iterator to the element following the last element

Return:

Iterator

Example:

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

public entries(): JsIterator source

Forward ES6 iterator for all values in ascending order.

Return:

JsIterator

Example:

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

public erase(iterator: Iterator) source

Removes value for the specified iterator.

Params:

NameTypeAttributeDescription
iterator Iterator

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}

public find(key: *): Iterator source

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

Params:

NameTypeAttributeDescription
key *

Return:

Iterator

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
}

public first(): * source

Return:

*

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

Example:

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

public forEach(callback: *) source

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.

Params:

NameTypeAttributeDescription
callback *

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}`);
});

public has(key: *): Boolean source

A boolean indicator whether set contains the specified key.

Params:

NameTypeAttributeDescription
key *

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

Return:

Boolean

Example:

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

public insertOrReplace(key: *): InsertionResult source

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

Params:

NameTypeAttributeDescription
key *

Return:

InsertionResult

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

Example:

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

public insertUnique(key: *): InsertionResult source

Adds a key if it doesn't exist

Params:

NameTypeAttributeDescription
key *

Return:

InsertionResult

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

Example:

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

public keys(): JsIterator source

Forward ES6 iterator for all keys in ascending order.

Return:

JsIterator

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().backward()) {
  console.log(k); // 3, 2, 1
}

public last(): * source

Return:

*

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

Example:

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

public lowerBound(key: *): Iterator source

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

Params:

NameTypeAttributeDescription
key *

Return:

Iterator

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();
}

public rbegin(): ReverseIterator source

Reverse iterator to the last element.

Return:

ReverseIterator

Example:

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

public rend(): ReverseIterator source

Reverse iterator pointing to before the first element.

Return:

ReverseIterator

Example:

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

public toString(): String source

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

Return:

String

public upperBound(key: *): Iterator source

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

Params:

NameTypeAttributeDescription
key *

Return:

Iterator

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();
}

public values(): JsITerator source

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

Return:

JsITerator

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().backward()) {
  console.log(v); // '3', '2', '1'
}