@saehrimnir/druidjs / Heap
Class: Heap<T>
Defined in: datastructure/Heap.js:8
Template
Type Parameters
| Type Parameter | Description |
|---|---|
T |
Constructors
Constructor
new Heap<T>(
elements?: T[] | null,
accessor: (d: T) => number,
comparator?: "max" | "min" | Comparator): Heap<T>;Defined in: datastructure/Heap.js:26
A heap is a datastructure holding its elements in a specific way, so that the top element would be the first entry of an ordered list.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
elements | T[] | null | null | Contains the elements for the Heap. elements can be null. |
accessor | (d: T) => number | undefined | Function returns the value of the element. |
comparator? | "max" | "min" | Comparator | "min" | Function returning true or false defining the wished order of the Heap, or String for predefined function. ("min" for a Min-Heap, "max" for a Max_heap). Default is "min" |
Returns
Heap<T>
See
https://en.wikipedia.org/wiki/Binary_heap
Properties
| Property | Type | Defined in |
|---|---|---|
_accessor | (d: T) => number | datastructure/Heap.js:28 |
_comparator | Comparator | datastructure/Heap.js:13 |
_container | { element: T; value: number; }[] | datastructure/Heap.js:10 |
Accessors
empty
Get Signature
get empty(): boolean;Defined in: datastructure/Heap.js:227
Returns false if the the heap has entries, true if the heap has no entries.
Returns
boolean
first
Get Signature
get first():
| {
element: T;
value: number;
}
| null;Defined in: datastructure/Heap.js:171
Returns the top entry of the heap without removing it.
Returns
| { element: T; value: number; } | null
Object consists of the element and its value (computed by accessor).
length
Get Signature
get length(): number;Defined in: datastructure/Heap.js:218
The size of the heap.
Returns
number
Methods
data()
data(): T[];Defined in: datastructure/Heap.js:200
Returns elements of container array.
Returns
T[]
Array consisting the elements.
iterate()
iterate(): Generator<T, void, unknown>;Defined in: datastructure/Heap.js:180
Yields the raw data
Returns
Generator<T, void, unknown>
Yields
Object consists of the element and its value (computed by accessor}).
pop()
pop():
| {
element: T;
value: number;
}
| null;Defined in: datastructure/Heap.js:150
Removes and returns the top entry of the heap.
Returns
| { element: T; value: number; } | null
Object consists of the element and its value (computed by accessor}).
push()
push(element: T): Heap<T>;Defined in: datastructure/Heap.js:111
Pushes the element to the heap.
Parameters
| Parameter | Type | Description |
|---|---|---|
element | T | - |
Returns
Heap<T>
raw_data()
raw_data(): {
element: T;
value: number;
}[];Defined in: datastructure/Heap.js:209
Returns the container array.
Returns
{ element: T; value: number; }[]
The container array.
toArray()
toArray(): T[];Defined in: datastructure/Heap.js:191
Returns the heap as ordered array.
Returns
T[]
Array consisting the elements ordered by comparator.
heapify()
static heapify<T>(
elements: T[],
accessor: (d: T) => number,
comparator?: "max" | "min" | Comparator): Heap<T>;Defined in: datastructure/Heap.js:62
Creates a Heap from an Array
Type Parameters
| Type Parameter | Description |
|---|---|
T |
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
elements | T[] | undefined | Contains the elements for the Heap. |
accessor | (d: T) => number | undefined | Function returns the value of the element. |
comparator? | "max" | "min" | Comparator | "min" | Function returning true or false defining the wished order of the Heap, or String for predefined function. ("min" for a Min-Heap, "max" for a Max_heap). Default is "min" |
Returns
Heap<T>