Heap

data-structures/heap. Heap

new Heap(cmp)

Minimum heap constructor.
Source:
Parameters:
Name Type Description
cmp function Function used for comparison between the elements.

Methods

add(value) → {Number}

Adds new element to the heap.

Complexity: O(log N).
Source:
Parameters:
Name Type Description
value Number | Object Value which will be inserted.
Returns:
Type:
Number
Index of the inserted value.

changeKey(index, value) → {Number}

Changes the key.

Complexity: O(log N).
Source:
Parameters:
Name Type Description
index Number Index of the value which should be changed.
value Number | Object New value according to the index.
Returns:
Type:
Number
New position of the element.

extract() → {Number|Object}

Removes and returns the current extremum value which is on the top of the heap.

Complexity: O(log N).
Source:
Returns:
Type:
Number | Object
The extremum value.

isEmpty() → {Boolean}

Checks or heap is empty.
Source:
Returns:
Type:
Boolean
Returns true if heap is empty.

top() → {Number|Object}

Returns current value which is on the top of the heap.

Complexity: O(1).
Source:
Returns:
Type:
Number | Object
Current top value.

update(node)

Updates a given node. This operation is useful in algorithms like Dijkstra, A* where we need to decrease/increase value of the given node.
Source:
Parameters:
Name Type Description
node Number | Object Node which should be updated.