Skip to content

BinaryHeap<T>

Guide: /guide/data-structures/binary-heap

Generic Types

T - type of collection elements

Methods

constructor(comparator?: (a: T, b: T) => number): BinaryHeap<T>

Creates a new BinaryHeap instance

Params:
NameTypeRequiredDefaultDescription
comparator(a: T, b: T) => number-Default comparatorFunction to determine element priority. Returns negative if a < b, positive if a > b, zero if equal.



isEmpty(): boolean

Returns true if the heap contains no elements



peek(): T

Retrieves the root element without removing it

Throws: CollectionIsEmptyException when heap is empty



insert(value: T): void

Adds a new value and restores heap properties

Params:
NameTypeRequiredDefaultDescription
valueT+-The value to insert



extractMin(): T

Removes and returns the root element (minimum based on comparator)

Throws: CollectionIsEmptyException when heap is empty



[Symbol.iterator](): IterableIterator<T>

Returns an iterator over the underlying array



size

Type: number

Current number of elements in the heap



MIT Licensed