R
C

BinaryHeap

v0.0.8tested

Binary heap backed by a flat array with configurable comparator

Signature

ts
class BinaryHeap<T> implements BinaryHeapLike<T>

Type Parameters

T

Properties

PropertyTypeDescription
lengthreadonlynumberGets the number of elements in the heap
isEmptyreadonlybooleanChecks if the heap is empty

Methods

push

Pushes an element into the heap

ts
public push(element: T): void
ParameterTypeDescription
elementTThe element to insert
pop

Removes and returns the root element (min or max depending on comparator)

ts
public pop(): T | undefined
ReturnsT | undefinedThe root element, or `undefined` if the heap is empty
peek

Returns the root element without removing it

ts
public peek(): T | undefined
ReturnsT | undefinedThe root element, or `undefined` if the heap is empty
clear

Removes all elements from the heap

ts
public clear(): this
ReturnsthisThe heap instance for chaining
toArray

Returns a shallow copy of the heap elements as an array (heap order, not sorted)

ts
public toArray(): T[]
ReturnsT[]Array of elements in heap order
toString

Returns a string representation of the heap

ts
public toString(): string
ReturnsstringString representation
[Symbol.iterator]

Iterator over heap elements in heap order

ts
public* [Symbol.iterator](): Iterator<T>
ReturnsIterator<T, any, any>
[Symbol.asyncIterator]

Async iterator over heap elements in heap order

ts
public async* [Symbol.asyncIterator](): AsyncIterator<T>
ReturnsAsyncIterator<T, any, any>