Skip to content

Commit 1fdb916

Browse files
committed
Binary Heap: Minor optimisation, add except for undefined node on decreaseKey
1 parent 8763e9e commit 1fdb916

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/binary-heap.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@
5656
};
5757

5858
BinaryHeap.prototype.decreaseKey = function (node, newKey) {
59+
if (typeof node === 'undefined') {
60+
throw "Cannot decrease key of non-existant node";
61+
}
5962
// Create a temp node for comparison
6063
if (this.compare(new Node(newKey, undefined), node.key) > 0) {
61-
throw "New key is larger than old key.";
64+
throw "New key is larger than old key";
6265
}
6366

6467
node.key = newKey;
@@ -160,9 +163,8 @@
160163
var temp = array[a];
161164
array[a] = array[b];
162165
array[b] = temp;
163-
var tempI = array[a].i;
164-
array[a].i = array[b].i;
165-
array[b].i = tempI;
166+
array[a].i = a;
167+
array[b].i = b;
166168
}
167169

168170
function getParent(i) {

0 commit comments

Comments
 (0)