heap.js icon indicating copy to clipboard operation
heap.js copied to clipboard

Heapify with custom comparator does not work

Open npow opened this issue 8 years ago • 1 comments

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)

npow avatar Oct 19 '16 00:10 npow

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());
}

kgilliam125 avatar Jan 28 '17 03:01 kgilliam125