Skip to content

jrwats/BinHeap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bin-heap-js

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

About

A simple JavaScript array-based BinaryHeap implementation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published