# KHeap

Defined in: [packages/heap/src/k-heap.ts:50](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/k-heap.ts#L50)

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

### A

`A`

## Properties

### kind

> `readonly` **kind**: `"k-min"` | `"k-max"`

Defined in: [packages/heap/src/k-heap.ts:52](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/k-heap.ts#L52)

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

***

### arity

> `readonly` **arity**: `number`

Defined in: [packages/heap/src/k-heap.ts:54](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/k-heap.ts#L54)

Number of children per node, fixed at construction.

***

### size

> `readonly` **size**: `number`

Defined in: [packages/heap/src/k-heap.ts:56](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/k-heap.ts#L56)

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

***

### peek

> `readonly` **peek**: () => `A`

Defined in: [packages/heap/src/k-heap.ts:64](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/k-heap.ts#L64)

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

Property syntax for the same variance reason as [push](/reference/knotkit/heap/interfaces/kheap/#push).

#### Returns

`A`

#### Throws

With code `EMPTY` when the heap is empty.

***

### pop

> `readonly` **pop**: () => `A`

Defined in: [packages/heap/src/k-heap.ts:73](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/k-heap.ts#L73)

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](/reference/knotkit/heap/interfaces/kheap/#push).

#### Returns

`A`

#### Throws

With code `EMPTY` when the heap is empty.

***

### replaceRoot

> `readonly` **replaceRoot**: (`value`) => `A`

Defined in: [packages/heap/src/k-heap.ts:78](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/k-heap.ts#L78)

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

#### Parameters

##### value

`A`

#### Returns

`A`

#### Throws

With code `EMPTY` when the heap is empty.

***

### push

> `readonly` **push**: (`value`) => `void`

Defined in: [packages/heap/src/k-heap.ts:88](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/k-heap.ts#L88)

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

##### value

`A`

#### Returns

`void`

***

### clear

> `readonly` **clear**: () => `void`

Defined in: [packages/heap/src/k-heap.ts:95](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/k-heap.ts#L95)

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](/reference/knotkit/heap/interfaces/kheap/#push).

#### Returns

`void`

***

### values

> `readonly` **values**: () => `IterableIterator`<`A`>

Defined in: [packages/heap/src/k-heap.ts:106](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/k-heap.ts#L106)

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](/reference/knotkit/heap/interfaces/kheap/#push).

#### Returns

`IterableIterator`<`A`>

***

### \[iterator]

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

Defined in: [packages/heap/src/k-heap.ts:108](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/k-heap.ts#L108)

The same walk as [values](/reference/knotkit/heap/interfaces/kheap/#values), as the same function, so `for (const value of heap)` works.

#### Returns

`IterableIterator`<`A`>
