Skip to content

Defined in: packages/heap/src/binary-heap.ts:59

A mutable binary heap: the extreme element under the factory’s order sits at the root, and pop extracts in that direction — minimum first for 'binary-min', maximum first for 'binary-max'.

All implementations mutate in place. Duplicate elements are allowed; elements that compare equal have unspecified relative extraction order.

The type parameter is invariant: a mutable container both consumes and produces A, so BinaryHeap<Dog> must not be assignable to BinaryHeap<Animal> — that would allow pushing a Cat into a structure typed as holding only dogs.

The order supplied at construction must be total, deterministic, pure, and non-throwing, and elements must not mutate order-relevant fields while resident; violating either can leave the structure permanently inconsistent with no error signal.

A

readonly kind: "binary-min" | "binary-max"

Defined in: packages/heap/src/binary-heap.ts:61

Storage layout and extraction direction. Either 'binary-min' or 'binary-max'.


readonly size: number

Defined in: packages/heap/src/binary-heap.ts:63

Number of elements. O(1). The heap is empty when this is 0.


readonly peek: () => A

Defined in: packages/heap/src/binary-heap.ts:71

Return the extreme element without removing it. O(1).

Property syntax for the same variance reason as push.

A

With code EMPTY when the heap is empty.


readonly pop: () => A

Defined in: packages/heap/src/binary-heap.ts:80

Remove and return the extreme element — the minimum for min kinds, the maximum for max kinds, under the factory order. O(log n).

Property syntax for the same variance reason as push.

A

With code EMPTY when the heap is empty.


readonly replaceRoot: (value) => A

Defined in: packages/heap/src/binary-heap.ts:85

Return the current root and replace it with the supplied value, preserving size. O(log n).

A

A

With code EMPTY when the heap is empty.


readonly push: (value) => void

Defined in: packages/heap/src/binary-heap.ts:96

Insert an element and restore the heap invariant. O(log n).

Declared in property syntax on purpose: method-syntax parameters compare bivariantly, which would let BinaryHeap<Dog> widen into Heap<Animal> through the union’s apparent members and defeat the invariance in out A exists to enforce. Property syntax keeps the parameter contravariant under strictFunctionTypes. Every callable member is declared the same way, so the interface reads uniformly.

A

void


readonly clear: () => void

Defined in: packages/heap/src/binary-heap.ts:103

Remove every element, keeping the kind, arity, and order. O(1) amortized: the backing array is truncated, never reallocated element by element.

Property syntax for the same variance reason as push.

void


readonly values: () => IterableIterator<A>

Defined in: packages/heap/src/binary-heap.ts:114

Walk the elements lazily in internal heap-array order, which is neither sorted nor insertion order: the extreme element comes first and every parent precedes its children. O(n) for a full walk, O(1) extra space. Does not mutate the heap. The walk reads the live array, so changing the heap while a walk is open is unsupported: the walk still ends, but which elements it yields is unspecified.

Property syntax for the same variance reason as push.

IterableIterator<A>


readonly [iterator]: () => IterableIterator<A>

Defined in: packages/heap/src/binary-heap.ts:116

The same walk as values, as the same function, so for (const value of heap) works.

IterableIterator<A>