Class

TreeMap

TreeMap()

TreeMap is an associative container that stores elements formed by a combination of a key value and a mapped value, following a specific order.

In a TreeMap, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ.

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. Map - Each element associates a key to a mapped value. Keys are meant to identify the elements whose main content is the mapped value. Unique keys - No two elements in the container can have equivalent keys.

Constructor

# new TreeMap()

View Source tree-map.ts, line 35

Example
let map = new TreeMap();
// add few values
map.set(1, 'a');
map.set(2, 'b');
// find a value by key
let v = map.get(1); // << 'a'
// print all key-value pairs
for (let [key, value] of map) {
  console.log(`key: ${key}, value: ${value}`);
}

Members

# 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-map.ts, line 257

# size

Number of key-value pairs in the map.

View Source tree-map.ts, line 202

Methods

# backwards() → {IterableIterator}

ES6 reverse iterator for all key-value pairs in descending order of the keys.

View Source tree-map.ts, line 247

reverse iterator for all elements in the container

IterableIterator
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let [key,value] of map.backwards()) {
  console.log(`key: ${key}, value: ${value}`);
}

# begin() → {MapIterator}

Forward iterator to the first element

View Source tree-map.ts, line 682

forward iterator pointing at the first node

MapIterator
Example
let m = new TreeMap();
...
for (let it = m.begin(); !it.equals(m.end()); it.next()) {
  console.log(`key: ${it.key}, value: ${it.value}`);
}

# clear()

Removes all key-value pairs.

View Source tree-map.ts, line 91

Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
map.clear();
console.log(map.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-map.ts, line 670

# delete(key) → {void}

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

Parameters:
Name Type Description
key K

Node's key

View Source tree-map.ts, line 542

void
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
map.delete(2);
console.log(map.toString()); // {1:A,3:C}

# end() → {MapIterator}

Forward iterator to the element following the last element

View Source tree-map.ts, line 694

iterator to the node after the last element

MapIterator
Example
let m = new TreeMap();
...
for (let it = m.begin(); !it.equals(m.end()); it.next()) {
  console.log(`key: ${it.key}, value: ${it.value}`);
}

# entries() → {IterableIterator}

Forward ES6 iterator for all key-value pairs in ascending order of the keys.

View Source tree-map.ts, line 553

forward iterator for all nodes in the container

IterableIterator
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let [key,value] of map.entries()) {
  console.log(`key: ${key}, value: ${value}`);
}

# erase(iterator) → {void}

Removes key-value pair for the specified iterator.

Parameters:
Name Type Description
iterator MapIterator

iterator pointing to the node to be removed

View Source tree-map.ts, line 757

void
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let it = map.find(2);
it.prev();
map.erase(it); // removes a node with key 1
console.log(map.toString()); // {2:B,3:C}

# find(key) → {MapIterator}

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

to check

View Source tree-map.ts, line 708

forward iterator to the node with the specified key

MapIterator
Example
let m = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
...
let it = m.find(1);
if (!it.equals(m.end())) {
  console.log(`key: ${it.key}, value: ${it.value}`); // 1, 'A'
}

# first() → {Array|undefined}

Returns first key/value pair of the container, or undefined if container is empty

View Source tree-map.ts, line 852

first key/value pair of the container, or undefined if container is empty

Array | undefined
Example
let m = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let first = m.first();
if (first) {
  let key = first[0];   // 1
  let value = first[1]; // 'A'
}

# forEach(callback) → {void}

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

Parameters:
Name Type Description
callback *

similar to forEach callbacks for standard JS maps

View Source tree-map.ts, line 566

void
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
map.forEach(function(value, key, container) {
  console.log(`key: ${key}, value: ${value}`);
});

# get(key) → {V|undefined}

Finds value associated with the specified key. If specified key does not exist then undefined is returned.

Parameters:
Name Type Description
key K

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

View Source tree-map.ts, line 577

the value associated with the given key

V | undefined
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let v = map.get(3); // 'C'
let v = map.get(4); // returns undefined

# has(key) → {boolean}

A boolean indicator whether map contains a key-value pair with the specified key

Parameters:
Name Type Description
key K

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

View Source tree-map.ts, line 587

returns true if given key exists in the container

boolean
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let b = map.get(3); // true

# insertOrReplace(key, value) → {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 to be added / replaced

value V

new value

View Source tree-map.ts, line 744

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

Example
let m = new TreeMap();
let res = m.insertOrReplace(1, 'A');
if (res.wasAdded) {
  console.log(`Inserted ${res.iterator.value}`); // prints A
}
res = m.insertOrReplace(1, 'B') // replaces value on the existing node
if (res.wasReplaced) {
  console.log(`Replaced ${res.iterator.key}`); // prints B
}

# insertUnique(key, value) → {InsertionResult}

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

Parameters:
Name Type Description
key K

key value to insert

value V

value to be associated with the key

View Source tree-map.ts, line 726

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

Example
let m = new TreeMap();
let res = m.insertUnique(1, 'A');
if (res.wasAdded) {
  console.log(`Inserted ${res.iterator.value}`); // prints A
}
res = m.insertUnique(1, 'B') // this step has no effect on the map
if (res.wasAdded) {
  console.log(`Inserted ${res.iterator.key}`); // not executed
}

# keys() → {IterableIterator}

Forward ES6 iterator for all keys in ascending order of the keys.

View Source tree-map.ts, line 604

forward iterator

IterableIterator
Example
// iterate all keys
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let k of map.keys()) {
  console.log(k); // 1, 2, 3
}
// iterate all keys in reverse order
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let k of map.keys().backwards()) {
  console.log(k); // 3, 2, 1
}

# last() → {Array|undefined}

Returns last key/value pair of the container, or undefined if container is empty

View Source tree-map.ts, line 865

last key/value pair of the container, or undefined if container is empty

Array | undefined
Example
let m = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let last = m.last();
if (last) {
  let key = last[0];   // 3
  let value = last[1]; // 'C'
}

# lowerBound(key) → {MapIterator}

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-map.ts, line 786

iterator pointing to the found node

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

let m = new TreeMap();
... // 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(m.upperBound(50));
let to = new ReverseIterator(m.lowerBound(0));
let it = from;
while (!it.equals(to)) {
  console.log(it.key);
  it.next();
}

# rbegin() → {MapIterator}

Returns iterator to the first element for reverse iterator

View Source tree-map.ts, line 798

iterator pointing to the node with the highest key

MapIterator
Example
let m = new TreeMap();
...
for (let it = m.rbegin(); !it.equals(m.rend()); it.next()) {
  console.log(`key: ${it.key}, value: ${it.value}`);
}

# rend() → {MapIterator}

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

View Source tree-map.ts, line 810

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

MapIterator
Example
let m = new TreeMap();
...
for (let it = m.rbegin(); !it.equals(m.rend()); it.next()) {
  console.log(`key: ${it.key}, value: ${it.value}`);
}

# set(key, value) → {void}

Adds or updates key-value pair to the map.

Parameters:
Name Type Description
key K

Key for the node to be added/updated

value V

New value

View Source tree-map.ts, line 615

void
Example
let map = new TreeMap();
map.set(1, 'A');

# size() → {number}

Number of key-value pairs in the map.

View Source tree-map.ts, line 621

number of elements in the container

number

# toString() → {string}

Serializes contents of the map in the form {key1:value1,key2:value2,...}

View Source tree-map.ts, line 871

serialized representation of the tree

string

# upperBound(key) → {MapIterator}

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

View Source tree-map.ts, line 839

iterator pointing to the found node

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

let m = new TreeMap();
... // 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(m.upperBound(50));
let to = new ReverseIterator(m.lowerBound(0));
let it = from;
while (!it.equals(to)) {
  console.log(it.key);
  it.next();
}

# values() → {IterableIterator}

Forward ES6 iterator for all values in ascending order of the keys.

View Source tree-map.ts, line 638

forward iterator for all values

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