0.0.6 • Published 3 years ago

bin-heap-js v0.0.6

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

BinHeap

A binary heap with a simple native-array-based implementation.

Constructor BinHeap([cmp])

The constructor takes an optional comparator. If provided, the comparator should behave similar to the comparator passed to Array.prototype.sort.

Specifically:

If cmp(a, b) < 0, then a comes before b

If cmp(a, b) > 0, then b comes before a

Example

const BinHeap = require('bin-heap-js');

// Priority queue where a greater priority is more important
let priorityQueue = new BinHeap((a,b) => b.priority - a.priority); // max-heap
priorityQueue.push({name: "job C", job: ..., priority: 5});
priorityQueue.push({name: "job A", job: ..., priority: 99});
priorityQueue.push({name: "job B", job: ..., priority: 10});
process(priorityQueue.pop()); // job A
process(priorityQueue.pop()); // job B
process(priorityQueue.pop()); // job C

DEFAULT

The default comparator provided will work as a min-heap for numbers:

(a,b) => a - b

Methods

  • push(item) Push item onto heap
  • pop() Pop "top-most" item on heap
  • peek() Return "top-most" item on heap
  • size() Number of elements in the heap
0.0.6

3 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago