BHeap

BHeap

Binary heap representation

Constructor

new BHeap(comparator)

Example
const heap = new Collections.BHeap();
// this creates a max heap by default.
// for a min heap, see @link above and swap 1 and -1
// FOR ALL EXAMPLES BELOW. ASSUME heap IS CLEARED BEFORE EACH EXAMPLE
Parameters:
Name Type Description
comparator function @see Global#defaultComp for examples

Methods

clear() → {undefined}

Empties the Heap
Returns:
Type
undefined

contains(dataopt) → {boolean}

Reports whether the BHeap contains the given data
Example
heap.insert(1).insert(2);
heap.contains(2) // true
Parameters:
Name Type Attributes Default Description
data * <optional>
null The data to search for
Returns:
True if the heap contains @param data and false otherwise
Type
boolean

extractRoot() → {*}

Removes the root of the BHeap and returns the data
Example
heap.insert(1).insert(2).insert(3);
let root = heap.extractRoot();
// root = 3;
Returns:
The extracted data
Type
*

insert(dataopt) → {BHeap}

Inserts the given data into the BHeap
Example
heap.insert(1).insert(2).insert(3).insert(3);
// this heap will contain both 3s

heap.extractRoot() // will be 3
Parameters:
Name Type Attributes Default Description
data * <optional>
null The data to insert into BHeap.
Returns:
A reference to the instance that this method was called
Type
BHeap

peek() → {*|undefined}

Retrieves the element staged to be removed next but does not remove it
Example
heap.insert(9);
heap.peek() // returns 9 and heap is still of size 1
Returns:
The set to be removed data or undefined if empty heap
Type
* | undefined

size()

Reports the number of elements in the BHeap.
Example
heap.size() // would be 0
Returns:
The BHeap instance's number of elements

toArray() → {Array}

Transforms the BHeap into an array
Example
heap.insert(1).insert(2);
heap.toArray() // will be [2, 1]
Returns:
The heap instance as an array
Type
Array