Typed API (TypeDoc) / BinaryHeap
Class: BinaryHeap<E>
Defined in: heap.ts:22
Example
const heap = new BinaryHeap(numberComparatorASC)
heap.insert(3)
heap.insert(1)
heap.extractMin() // -> 1Since
2.0.0
Type Parameters
E
E
Value type.
Implements
ICollection<E>
Constructors
Constructor
new BinaryHeap<
E>(comparator,elements?):BinaryHeap<E>
Defined in: heap.ts:37
Parameters
comparator
Comparator<E>
Ordering strategy (ascending produces a min-heap).
elements?
Iterable<E, any, any>
Optional seed data; inserted in comparator order.
Returns
BinaryHeap<E>
Properties
comparator
comparator:
Comparator<E>
Defined in: heap.ts:31
Comparator used for equality/sort checks.
Implementation of
size
size:
number=0
Defined in: heap.ts:27
Current element count.
Implementation of
Methods
[iterator]()
[iterator]():
Iterator<E>
Defined in: heap.ts:188
Iterate elements in ascending comparator order.
Returns
Iterator<E>
Iterator snapshot; future mutations do not affect ongoing iteration.
Implementation of
ICollection.[iterator]
add()
add(
element):void
Defined in: heap.ts:93
Alias for insert.
Parameters
element
E
Returns
void
Implementation of
addAll()
addAll(
collection):void
Defined in: heap.ts:100
Append every element from another collection.
Parameters
collection
ICollection<E>
Returns
void
Remarks
Complexity: O(n + m) where m is collection.size.
Implementation of
clear()
clear():
void
Defined in: heap.ts:109
Remove all entries.
Returns
void
Remarks
Complexity: O(n)
Implementation of
contains()
contains(
element):boolean
Defined in: heap.ts:124
Test membership using the comparator when available.
Parameters
element
E
Returns
boolean
Remarks
Complexity: O(n) worst case
Implementation of
extractMin()
extractMin():
E
Defined in: heap.ts:66
Remove and return the top element according to the comparator.
Returns
E
Extracted value.
Throws
When the heap is empty.
Remarks
Complexity: O(log n)
insert()
insert(
element):void
Defined in: heap.ts:52
Insert an element into the heap.
Parameters
element
E
Value to insert (ignored when undefined).
Returns
void
Remarks
Complexity: O(log n)
isEmpty()
isEmpty():
boolean
Defined in: heap.ts:117
Check for emptiness.
Returns
boolean
true when size === 0.
Implementation of
peek()
peek():
E
Defined in: heap.ts:85
Read the current top element without removing it.
Returns
E
Heap front value.
Throws
When empty.
Remarks
Complexity: O(1)
remove()
remove(
target,isIndex):number|E
Defined in: heap.ts:161
Remove by value or index.
Parameters
target
Element or index.
number | E
isIndex
boolean = true
When true, treat e as index.
Returns
number | E
Removed element or index of removal.
Remarks
Complexity: O(n) worst case.
Implementation of
reverseIterator()
reverseIterator():
Generator<E>
Defined in: heap.ts:202
Iterates elements from the most recently added to the earliest.
Returns
Generator<E>
Implementation of
sort()
sort(
cmp?):void
Defined in: heap.ts:144
Rebuild the heap using a different comparator.
Parameters
cmp?
Comparator<E>
Optional comparator override.
Returns
void
Throws
If neither argument nor existing comparator are set.
Remarks
Complexity: O(n log n)