Skip to main content
Module

x/structurae/index.ts>BinaryHeap

Data structures for high-performance JavaScript applications.
Latest
class BinaryHeap
extends Array<T>
import { BinaryHeap } from "https://deno.land/x/structurae@4.0.1/index.ts";

Extends Array to implement the Binary Heap data structure. // TODO (docs) document priority queue operations // todo add heapsort?

Constructors

new
BinaryHeap(...args: any[])

Methods

has(index: number): boolean

Check whether the index is whithin the heap.

heapify(): this

Restores the binary heap.

isHeap(): boolean

Checks whether the array is a valid binary heap.

left(index: number): T

Returns the left child of an element at a given index.

parent(index: number): T

Returns the parent of an element at a given index.

push(...elements: Array<T>): number

Adds items to the heap.

replace(element: T): T

Returns the first (min/max) element of the heap and replaces it with a given element.

right(index: number): T

Returns the right child of an element at a given index.

shift(): T | undefined

Extracts the first element of the heap.

siftDown(start: number): void
siftUp(start: number): void
splice(
start: number,
deleteCount?: number,
...items: Array<T>,
): Array<T>

Changes elements of the heap.

unshift(...items: Array<T>): number

Adds elements to the heap.

update(index: number): void

Updates the position of an element inside the heap.

Static Properties

readonly
[Symbol.species]: ArrayConstructor

Static Methods

compare(a: unknown, b: unknown): boolean

The comparator function used by the heap.

from<T>(iterable: Iterable<T> | ArrayLike<T>): BinaryHeap<T>

Creates a new BinaryHeap from a given array-like object.

getLeftIndex(index: number): number

Get left child index from parent index.

getParentIndex(index: number): number

Get left child index from parent index.

getRightIndex(index: number): number

Get right child index from parent index.

isHeap<T>(heap: ArrayLike<T>): boolean

Checks if a given collection is a valid binary heap.

of<T>(...elements: Array<T>): BinaryHeap<T>

Creates a new BinaryHeap with a variable number of arguments, regardless of number or type of the arguments.