Defined in: packages/heap/src/k-heap.ts:50
A mutable k-ary heap: like a binary heap, but every node holds up to k
children. The extreme element under the factory’s order sits at the root,
and pop extracts in that direction — minimum first for 'k-min',
maximum first for 'k-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 KHeap<Dog> must not be assignable to KHeap<Animal>.
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.
Type Parameters
Section titled “Type Parameters”A
Properties
Section titled “Properties”
readonlykind:"k-min"|"k-max"
Defined in: packages/heap/src/k-heap.ts:52
Storage layout and extraction direction. Either 'k-min' or 'k-max'.
readonlyarity:number
Defined in: packages/heap/src/k-heap.ts:54
Number of children per node, fixed at construction.
readonlysize:number
Defined in: packages/heap/src/k-heap.ts:56
Number of elements. O(1). The heap is empty when this is 0.
readonlypeek: () =>A
Defined in: packages/heap/src/k-heap.ts:64
Return the extreme element without removing it. O(1).
Property syntax for the same variance reason as push.
Returns
Section titled “Returns”A
Throws
Section titled “Throws”With code EMPTY when the heap is empty.
readonlypop: () =>A
Defined in: packages/heap/src/k-heap.ts:73
Remove and return the extreme element — the minimum for min kinds, the maximum for max kinds, under the factory order. O(k log_k n).
Property syntax for the same variance reason as push.
Returns
Section titled “Returns”A
Throws
Section titled “Throws”With code EMPTY when the heap is empty.
replaceRoot
Section titled “replaceRoot”
readonlyreplaceRoot: (value) =>A
Defined in: packages/heap/src/k-heap.ts:78
Return the current root and replace it with the supplied value, preserving size. O(k log_k n).
Parameters
Section titled “Parameters”A
Returns
Section titled “Returns”A
Throws
Section titled “Throws”With code EMPTY when the heap is empty.
readonlypush: (value) =>void
Defined in: packages/heap/src/k-heap.ts:88
Insert an element and restore the heap invariant. O(log_k n) — sift-up compares once per level, and a k-ary tree has fewer levels than a binary one.
Declared in property syntax for the same reason as BinaryHeap#push:
method-syntax bivariance would let KHeap<Dog> widen into
Heap<Animal> through the union’s apparent members.
Parameters
Section titled “Parameters”A
Returns
Section titled “Returns”void
readonlyclear: () =>void
Defined in: packages/heap/src/k-heap.ts:95
Remove every element, keeping the kind, arity, and order. O(1): the backing array is truncated.
Property syntax for the same variance reason as push.
Returns
Section titled “Returns”void
values
Section titled “values”
readonlyvalues: () =>IterableIterator<A>
Defined in: packages/heap/src/k-heap.ts:106
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.
Returns
Section titled “Returns”IterableIterator<A>
[iterator]
Section titled “[iterator]”
readonly[iterator]: () =>IterableIterator<A>
Defined in: packages/heap/src/k-heap.ts:108
The same walk as values, as the same function, so for (const value of heap) works.
Returns
Section titled “Returns”IterableIterator<A>