Skip to content

Latest commit

 

History

History
407 lines (232 loc) · 7.86 KB

heap.md

File metadata and controls

407 lines (232 loc) · 7.86 KB

dastal - v5.0.0 / Heap

Interface: Heap<T>

A specialized tree-based data structure that satisfies the heap property (source).

Heap property: For any given node N, the key (e.g. value) of N is greater than or equal to the key of its children.

In a heap, the highest priority element (relative to its ordering) is always stored at the root. However, a heap is not a sorted structure; it can be regarded as being partially ordered. The heap property only applies between a parent node and its descendants. There is no implied ordering between siblings or cousins and no implied sequence for an ordered traversal.

A heap is a useful data structure when it is necessary to repeatedly remove the object with the highest priority. In the sense, it can be used to implement a priority queue.

At a Glance

Iterate

  • Iterate the heap: {@link [Symbol.iterator]}
  • Iterate the heap in sorted order: sorted

Get

  • Get the size of the heap: size
  • Get the top element: peek
  • Check if the heap contains a given element: contains
  • Get the heap's sorting method: comparator

Set

Add

Remove

  • Remove the top element: pop
  • Delete a given element: delete
  • Remove all elements: clear

Add & Remove

  • Add and then remove the top element: pushPop
  • Remove the top element and then add an element: replace

Type parameters

Name
T

Hierarchy

Implemented by

Table of contents

Properties

Methods

Properties

size

Readonly size: number

The number of elements in the collection.

Inherited from

Collection.size

Defined in

src/collection/collection.ts:5

Methods

[iterator]

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

Returns

Iterator<T, any, undefined>

Inherited from

Collection.[iterator]

Defined in

node_modules/typescript/lib/lib.es2015.iterable.d.ts:51


addAll

addAll(elements): number

Insert a set of elements into the heap.

Parameters

Name Type Description
elements Iterable<T> The elements to insert.

Returns

number

The new size of the list.

Defined in

src/heap/heap.ts:57


clear

clear(): void

Removes all elements.

Returns

void

Defined in

src/heap/heap.ts:61


comparator

comparator(): CompareFn<T>

Returns

CompareFn<T>

The function with which elements are sorted

Inherited from

Sorted.comparator

Defined in

src/index.ts:61


contains

contains(element): boolean

Check if an element is in the heap.

Parameters

Name Type Description
element T The element to find.

Returns

boolean

true if the element was found, otherwise false.

Defined in

src/heap/heap.ts:69


delete

delete(element): boolean

Delete an element from the heap.

Parameters

Name Type Description
element T The element to delete.

Returns

boolean

true if the element was found and deleted, otherwise false.

Defined in

src/heap/heap.ts:77


merge

merge(heap): Heap<T>

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

Parameters

Name Type Description
heap Heap<T> The heap to join with.

Returns

Heap<T>

The heap.

Defined in

src/heap/heap.ts:86


peek

peek(): undefined | T

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

Returns

undefined | T

The element at the top of the heap or undefined if empty.

Defined in

src/heap/heap.ts:92


pop

pop(): undefined | T

Remove the top of the heap (AKA extract).

Returns

undefined | T

The element at the top of the heap or undefined if empty.

Defined in

src/heap/heap.ts:98


push

push(element): number

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

Parameters

Name Type Description
element T The element to be inserted.

Returns

number

The new size of the heap.

Defined in

src/heap/heap.ts:106


pushPop

pushPop(element): T

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

Parameters

Name Type Description
element T The element to be inserted.

Returns

T

The element at the top of the heap.

Defined in

src/heap/heap.ts:114


replace

replace(element): undefined | T

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

Parameters

Name Type Description
element T The element to be inserted.

Returns

undefined | T

The element at the top of the heap or undefined if empty.

Defined in

src/heap/heap.ts:122


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>

Defined in

src/heap/heap.ts:128


update

update(curElement, newElement): boolean

Update a specific element.

Parameters

Name Type Description
curElement T The element to update.
newElement T The new element to insert.

Returns

boolean

true if curElement was found and updated, otherwise false.

Defined in

src/heap/heap.ts:137