heap.js
heap.js copied to clipboard
Heapify with custom comparator does not work
Test case below.
var Heap = require('heap');
var cmp = function (A, B) { return B - A; };
var L = [3, 1, 2];
Heap.heapify(L, cmp);
while (L.length) {
console.log(Heap.pop(L));
}
Produces:
3
1
2
Instead of (3, 2, 1)
Looking through the code, the static method Heap.heapify returns an array sorted using your comparer (while also munging the array you pass it as mentioned in #18 ). If you want to use it similar to your test case you can do
var Heap = require('heap');
var cmp = function (A, B) { return B - A; };
var L = [3, 1, 2];
var L2 = Heap.heapify(L, cmp);
while (L2.length) {
console.log(L2.pop());
}