A simple binary heap

npm install binaryheap
9 downloads in the last day
168 downloads in the last week
814 downloads in the last month


Basic binary heap tree using linked lists


var BinaryHeap = require('binaryheap');
var heap = new BinaryHeap();

var a = [6, 5, 3, 1, 8, 7, 2, 4];

a.forEach(function (k) {
  heap.insert({ value: k }, k);


while (heap.length) {
  console.log('popping', heap.pop().value);

By default it stores as a max-heap, if you pass truthy to the constructor though it will behave as a min-heap.


  • insert(obj, key) -- obj can be any new or existing object, and key is any value that behaves sanely with > or <
  • pop() -- removes and returns the maximum or minimum object from the root of the heap
  • remove(obj) -- removes a previously inserted object from the heap
  • print() -- mostly for debugging purposes prints a graphviz dot style digraph to confirm ordering


  • length -- number of objects currently in the heap
npm loves you