# BinaryHeap

Defined in: [packages/heap/src/binary-heap.ts:59](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/binary-heap.ts#L59)

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.

## Type Parameters

### A

`A`

## Properties

### kind

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

Defined in: [packages/heap/src/binary-heap.ts:61](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/binary-heap.ts#L61)

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

***

### size

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

Defined in: [packages/heap/src/binary-heap.ts:63](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/binary-heap.ts#L63)

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

***

### peek

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

Defined in: [packages/heap/src/binary-heap.ts:71](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/binary-heap.ts#L71)

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

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

#### Returns

`A`

#### Throws

With code `EMPTY` when the heap is empty.

***

### pop

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

Defined in: [packages/heap/src/binary-heap.ts:80](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/binary-heap.ts#L80)

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

#### Returns

`A`

#### Throws

With code `EMPTY` when the heap is empty.

***

### replaceRoot

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

Defined in: [packages/heap/src/binary-heap.ts:85](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/binary-heap.ts#L85)

Return the current root and replace it with the supplied value, preserving size. O(log 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/binary-heap.ts:96](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/binary-heap.ts#L96)

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.

#### Parameters

##### value

`A`

#### Returns

`void`

***

### clear

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

Defined in: [packages/heap/src/binary-heap.ts:103](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/binary-heap.ts#L103)

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

#### Returns

`void`

***

### values

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

Defined in: [packages/heap/src/binary-heap.ts:114](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/binary-heap.ts#L114)

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/binaryheap/#push).

#### Returns

`IterableIterator`<`A`>

***

### \[iterator]

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

Defined in: [packages/heap/src/binary-heap.ts:116](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/binary-heap.ts#L116)

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

#### Returns

`IterableIterator`<`A`>
