Class BinaryHeap<T>
Type Parameters
Implements
- Iterable<T>
Constructors
constructor
- new
Binary <T>(compare?): BinaryHeap<T>Heap Construct an empty binary heap.
Type Parameters
Parameters
Returns BinaryHeap<T>
Accessors
length
- get length(): number
The count of values stored in the binary heap.
The complexity of this operation is O(1).
Returns number
The count of values stored in the binary heap.
Methods
[iterator]
clear
drain
- drain(): IterableIterator<T, any, any>
Create an iterator that retrieves values from the binary heap in order from greatest to least. The binary heap is drained in the process.
To avoid draining the binary heap, create a copy using BinaryHeap.from and then call BinaryHeap.prototype.drain on the copy.
Returns IterableIterator<T, any, any>
An iterator for retrieving and removing values from the binary heap.
isEmpty
peek
pop
- pop(): undefined | T
Remove the greatest value from the binary heap and return it, or return undefined if the heap is empty.
Returns undefined | T
The greatest value from the binary heap, or undefined if the heap is empty.
Example: Removing the greatest value from the binary heap
import { BinaryHeap } from "@std/data-structures";
import { assertEquals } from "@std/assert";
const heap = BinaryHeap.from([4, 1, 3, 5, 2]);
assertEquals(heap.pop(), 5);
assertEquals([...heap], [4, 3, 2, 1]);The complexity of this operation is on average and worst case O(log n), where n is the count of values stored in the binary heap.
push
- push(...values): number
Add one or more values to the binary heap, returning the new length of the heap.
The complexity of this operation is O(1) on average and O(log n) in the worst case, where n is the count of values stored in the binary heap.
Parameters
Rest...values: T[]The values to add to the binary heap.
Returns number
The new length of the binary heap.
toArray
Staticfrom
- from<T>(collection, options?): BinaryHeap<T>
Creates a new binary heap from an array like, an iterable object, or an existing binary heap.
A custom comparison function can be provided to sort the values in a specific order. By default, the values are sorted in descending order, unless a BinaryHeap is passed, in which case the comparison function is copied from the input heap.
Type Parameters
Parameters
Returns BinaryHeap<T>
A new binary heap containing the values from the passed collection.
Example: Creating a binary heap from an array like
import { BinaryHeap } from "@std/data-structures";
const heap = BinaryHeap.from([4, 1, 3, 5, 2]);Example: Creating a binary heap from an iterable object
import { BinaryHeap } from "@std/data-structures";
const heap = BinaryHeap.from((function*() { yield* [4, 1, 3, 5, 2]; })());Example: Creating a binary heap from an existing binary heap
import { BinaryHeap } from "@std/data-structures";
const heap = BinaryHeap.from([4, 1, 3, 5, 2]);
const copy = BinaryHeap.from(heap);- from<T, U, V>(collection, options): BinaryHeap<U>
Creates a new binary heap from an array like, an iterable object, or an existing binary heap.
A custom mapping function can be provided to transform the values before inserting them into the heap.
A custom comparison function can be provided to sort the values in a specific order. By default, the values are sorted in descending order, unless a BinaryHeap is passed, in which case the comparison function is copied from the input heap. The comparison operator is used to sort the values in the heap after mapping the values.
Type Parameters
Parameters
- collection: ArrayLike<T> | Iterable<T, any, any> | BinaryHeap<T>
An array like, an iterable object, or an existing binary heap.
- options: {
compare?: ((a: U, b: U) => number);
map: ((value: T, index: number) => U);
thisArg?: V;
}The options object to customize the mapping and comparison functions. The
thisArgproperty can be used to set thethisvalue when calling the mapping function.
Returns BinaryHeap<U>
A new binary heap containing the mapped values from the passed collection.
- collection: ArrayLike<T> | Iterable<T, any, any> | BinaryHeap<T>
A priority queue implemented with a binary heap. The heap is in descending order by default, using JavaScript's built-in comparison operators to sort the values.
Example: Usage
Typeparam
T The type of the values stored in the binary heap.