Skip to content

Commit 4240992

Browse files
committed
Add update method to the heap
1 parent 778787a commit 4240992

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/data-structures/heap.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@
7676
return parent;
7777
};
7878

79+
/**
80+
* Updates given node. This operation is useful
81+
* in algorithms like Dijkstra, A* where we need
82+
* to decrease/increase the value of givne node.
83+
*/
84+
Heap.prototype.update = function (node) {
85+
var idx = this._heap.indexOf(node);
86+
if (idx >= 0) {
87+
this.changeKey(idx, node);
88+
}
89+
};
90+
7991
/**
8092
* Adds new element to the heap. Complexity O(log n).
8193
*

0 commit comments

Comments
 (0)