Skip to content

@saehrimnir/druidjs / Heap

Class: Heap<T>

Defined in: datastructure/Heap.js:8

Template

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:227

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: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

ts
get length(): number;

Defined in: datastructure/Heap.js:218

The size of the heap.

Returns

number

Methods

data()

ts
data(): T[];

Defined in: datastructure/Heap.js:200

Returns elements of container array.

Returns

T[]

Array consisting the elements.


iterate()

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

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

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

Defined in: datastructure/Heap.js:111

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:209

Returns the container array.

Returns

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

The container array.


toArray()

ts
toArray(): T[];

Defined in: datastructure/Heap.js:191

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>