Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heapify with custom comparator does not work #20

Open
npow opened this issue Oct 19, 2016 · 1 comment
Open

Heapify with custom comparator does not work #20

npow opened this issue Oct 19, 2016 · 1 comment

Comments

@npow
Copy link

npow commented Oct 19, 2016

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)

@kgilliam125
Copy link

kgilliam125 commented Jan 28, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants