C
BinaryHeap
v0.0.8testedBinary heap backed by a flat array with configurable comparator
Signature
ts
class BinaryHeap<T> implements BinaryHeapLike<T>Type Parameters
TProperties
| Property | Type | Description |
|---|---|---|
lengthreadonly | number | Gets the number of elements in the heap |
isEmptyreadonly | boolean | Checks if the heap is empty |
Methods
pushPushes an element into the heap
ts
public push(element: T): void| Parameter | Type | Description |
|---|---|---|
element | T | The element to insert |
popRemoves and returns the root element (min or max depending on comparator)
ts
public pop(): T | undefinedReturns
T | undefinedThe root element, or `undefined` if the heap is emptypeekReturns the root element without removing it
ts
public peek(): T | undefinedReturns
T | undefinedThe root element, or `undefined` if the heap is emptyclearRemoves all elements from the heap
ts
public clear(): thisReturns
thisThe heap instance for chainingtoArrayReturns a shallow copy of the heap elements as an array (heap order, not sorted)
ts
public toArray(): T[]Returns
T[]Array of elements in heap ordertoStringReturns a string representation of the heap
ts
public toString(): stringReturns
stringString representation[Symbol.iterator]Iterator over heap elements in heap order
ts
public* [Symbol.iterator](): Iterator<T>Returns
Iterator<T, any, any>[Symbol.asyncIterator]Async iterator over heap elements in heap order
ts
public async* [Symbol.asyncIterator](): AsyncIterator<T>Returns
AsyncIterator<T, any, any>