Skip to content

Latest commit

 

History

History
436 lines (242 loc) · 8.02 KB

binaryheap.md

File metadata and controls

436 lines (242 loc) · 8.02 KB

dastal - v5.0.0 / BinaryHeap

Class: BinaryHeap<T>

A binary heap is a heap implemented as a binary tree with an additional shape property (source).

Shape property: Must be a complete binary tree. This means all levels of the tree (except possibly the last one) are fully filled. If the last level of the tree is incomplete, the nodes of that level are filled from left to right.

Complexity

Property Average Worst
Space O(n) O(n)
Push O(1) O(log n)
Peek O(1) O(1)
Pop O(log n) O(log n)
Search O(n) O(n)

Type parameters

Name
T

Implements

Table of contents

Constructors

Accessors

Methods

Constructors

constructor

new BinaryHeap<T>(compareFn, elements?)

Instantiate a heap.

Type parameters

Name
T

Parameters

Name Type Description
compareFn CompareFn<T> The function to determine the order of elements.
elements? Iterable<T> A set of elements to initialize the list with.

Defined in

src/heap/binaryHeap.ts:33

Accessors

size

get size(): number

The number of elements in the collection.

Returns

number

Implementation of

Heap.size

Defined in

src/heap/binaryHeap.ts:187

Methods

[iterator]

[iterator](): Iterator<T, any, undefined>

Receive an iterator through the list.

Note: Unexpected behavior can occur if the collection is modified during iteration.

Returns

Iterator<T, any, undefined>

An iterator through the list

Implementation of

Heap.[iterator]

Defined in

src/heap/binaryHeap.ts:214


addAll

addAll(elements): number

Insert a set of elements into the heap.

Parameters

Name Type
elements Iterable<T>

Returns

number

Implementation of

Heap.addAll

Defined in

src/heap/binaryHeap.ts:46


clear

clear(): void

Removes all elements.

Returns

void

Implementation of

Heap.clear

Defined in

src/heap/binaryHeap.ts:74


comparator

comparator(): CompareFn<T>

Returns

CompareFn<T>

Implementation of

Heap.comparator

Defined in

src/heap/binaryHeap.ts:78


contains

contains(element): boolean

Check if an element is in the heap.

Parameters

Name Type
element T

Returns

boolean

Implementation of

Heap.contains

Defined in

src/heap/binaryHeap.ts:82


delete

delete(element): boolean

Delete an element from the heap.

Parameters

Name Type
element T

Returns

boolean

Implementation of

Heap.delete

Defined in

src/heap/binaryHeap.ts:86


merge

merge(heap): BinaryHeap<T>

Join with a different heap and modify the existing heap to contain elements of both. Does not modify the input.

Parameters

Name Type
heap Heap<T>

Returns

BinaryHeap<T>

Implementation of

Heap.merge

Defined in

src/heap/binaryHeap.ts:106


peek

peek(): undefined | T

Retrieves, but does not remove, the top of the heap.

Returns

undefined | T

Implementation of

Heap.peek

Defined in

src/heap/binaryHeap.ts:125


pop

pop(): undefined | T

Remove the top of the heap (AKA extract).

Returns

undefined | T

Implementation of

Heap.pop

Defined in

src/heap/binaryHeap.ts:129


push

push(value): number

Inserts an element into the heap (AKA insert, add).

Parameters

Name Type
value T

Returns

number

Implementation of

Heap.push

Defined in

src/heap/binaryHeap.ts:148


pushPop

pushPop(value): T

Insert an element and then remove the top of the heap.

Parameters

Name Type
value T

Returns

T

Implementation of

Heap.pushPop

Defined in

src/heap/binaryHeap.ts:157


replace

replace(value): undefined | T

Remove the top of the heap and then insert a new element (AKA popPush).

Parameters

Name Type
value T

Returns

undefined | T

Implementation of

Heap.replace

Defined in

src/heap/binaryHeap.ts:170


sorted

sorted(): Iterable<T>

Iterate through the heap in sorted order.

Note: Unexpected behavior can occur if the collection is modified during iteration.

Returns

Iterable<T>

Implementation of

Heap.sorted

Defined in

src/heap/binaryHeap.ts:191


update

update(curElement, newElement): boolean

Update a specific element.

Parameters

Name Type
curElement T
newElement T

Returns

boolean

Implementation of

Heap.update

Defined in

src/heap/binaryHeap.ts:218