Constructor
new Heap(cmp)
Minimum heap constructor.
Parameters:
| Name | Type | Description |
|---|---|---|
cmp |
function | Function used for comparison between the elements. |
- Source:
Methods
add(value) → {Number}
Adds new element to the heap.
Complexity: O(log N).
Complexity: O(log N).
Parameters:
| Name | Type | Description |
|---|---|---|
value |
Number | Object | Value which will be inserted. |
- Source:
Returns:
Index of the inserted value.
- Type
- Number
changeKey(index, value) → {Number}
Changes the key.
Complexity: O(log N).
Complexity: O(log N).
Parameters:
| Name | Type | Description |
|---|---|---|
index |
Number | Index of the value which should be changed. |
value |
Number | Object | New value according to the index. |
- Source:
Returns:
New position of the element.
- Type
- Number
extract() → {Number|Object}
Removes and returns the current extremum value
which is on the top of the heap.
Complexity: O(log N).
Complexity: O(log N).
- Source:
Returns:
The extremum value.
- Type
- Number | Object
isEmpty() → {Boolean}
Checks or heap is empty.
- Source:
Returns:
Returns true if heap is empty.
- Type
- Boolean
top() → {Number|Object}
Returns current value which is on the top of the heap.
Complexity: O(1).
Complexity: O(1).
- Source:
Returns:
Current top value.
- Type
- Number | Object
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.
Parameters:
| Name | Type | Description |
|---|---|---|
node |
Number | Object | Node which should be updated. |
- Source: