Binary Heap
Simple js implementation of a binary heap. Mostly used for the heapsort algorithm, or as a priority queue.
Installation
$ npm install --save @vaalentin/binary-heap
Usage
import BinaryHeap from '@vaalentin/binary-heap';
const heap = new BinaryHeap((a, b) => {
return a.length - b.length;
});
heap.push('abcd');
heap.push('ab');
const value = heap.pop(); // ab
API
heap = new BinaryHeap(fn)
Creates a new heap, where fn
is an optional comparison function.
The comparison function takes the same form as the one for Array.sort.
function compare(a, b) {
if (a is less than b by some ordering criterion) {
return -1;
}
if (a is greater than b by the ordering criterion) {
return 1;
}
// a must be equal to b
return 0;
}
isEmpty = heap.isEmpty()
heap.push(node)
Push a new node to the tree.
node = heap.pop()
Pop the first node from the tree.
heap.update(node)
Update a specific node.
node.value = 12; // update value
heap.update(node); // update node
heap.dispose()
License
MIT, see LICENSE.md for more details.