Skip to content

@saehrimnir/druidjs / Heap

Class: Heap<T> ​

Defined in: datastructure/Heap.js:8

Template ​

T

Type Parameters ​

Type ParameterDescription
T

Constructors ​

Constructor ​

ts
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 ​

ParameterTypeDefault valueDescription
elementsT[] | nullnullContains the elements for the Heap. elements can be null.
accessor(d: T) => numberundefinedFunction 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 ​

PropertyTypeDefined in
_accessor(d: T) => numberdatastructure/Heap.js:28
_comparatorComparatordatastructure/Heap.js:13
_container{ element: T; value: number; }[]datastructure/Heap.js:10

Accessors ​

empty ​

Get Signature ​

ts
get empty(): boolean;

Defined in: datastructure/Heap.js:237

Returns false if the the heap has entries, true if the heap has no entries.

Returns ​

boolean


first ​

Get Signature ​

ts
get first(): 
  | {
  element: T;
  value: number;
}
  | null;

Defined in: datastructure/Heap.js:181

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 ​

ts
get length(): number;

Defined in: datastructure/Heap.js:228

The size of the heap.

Returns ​

number

Methods ​

data() ​

ts
data(): T[];

Defined in: datastructure/Heap.js:210

Returns elements of container array.

Returns ​

T[]

Array consisting the elements.


iterate() ​

ts
iterate(): Generator<T, void, unknown>;

Defined in: datastructure/Heap.js:190

Yields the raw data

Returns ​

Generator<T, void, unknown>

Yields ​

Object consists of the element and its value (computed by accessor}).


pop() ​

ts
pop(): 
  | {
  element: T;
  value: number;
}
  | null;

Defined in: datastructure/Heap.js:160

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() ​

ts
push(element: T): Heap<T>;

Defined in: datastructure/Heap.js:112

Pushes the element to the heap.

Parameters ​

ParameterTypeDescription
elementT-

Returns ​

Heap<T>


raw_data() ​

ts
raw_data(): {
  element: T;
  value: number;
}[];

Defined in: datastructure/Heap.js:219

Returns the container array.

Returns ​

{ element: T; value: number; }[]

The container array.


toArray() ​

ts
toArray(): T[];

Defined in: datastructure/Heap.js:201

Returns the heap as ordered array.

Returns ​

T[]

Array consisting the elements ordered by comparator.


heapify() ​

ts
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 ParameterDescription
T

Parameters ​

ParameterTypeDefault valueDescription
elementsT[]undefinedContains the elements for the Heap.
accessor(d: T) => numberundefinedFunction 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>