js-heap
A javascript heap implementation
Last updated 12 years ago by thauburger .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install js-heap 
SYNC missed versions from official npm registry.

js-heap

js-heap is a flexible, value-agnostic heap for Javascript. It provides a simple interface and allows users to define custom comparison functions for heap sorting.

Installation

npm install js-heap

Usage

To get started, require js-heap, and create a new Heap instance:

var Heap = require('js-heap');

var h = new Heap(function(a,b) {
  return a - b;
});

The comparison function behaves like a sort function you would pass into a standard Javascript Array.

The default comparison function is the one shown above - the heap is organized so that the highest numeric priority is at the top.

Heap Values

A node in the heap can be whatever you'd like:

// Example node
var node = {
  priority: 1,
  value: 'ABCD',
}

h.push(node);

You can create a custom sort function to compare arbitrary nodes:

var h = new Heap(function(a, b) {
  return a.priority - b.priority;
});

##.push(node) Pushes a new node onto the heap. A "node" value can be a primitive or an arbitrary Javascript object. Pushed nodes are sorted based on the provided sort function.

h.push(1);
h.push(2);
...

##.pop() Removes and returns the top node in the heap. If the heap is empty, null is returned.

// Returns 2
h.pop();

// Returns 1
h.pop();

// Returns null
h.pop();
...

##.peek() Returns the top node in the heap. The node remains on top of the heap.

h.push(1);

// Returns 1
h.peek();

// Still returns 1
h.peek();
...

##.isEmpty() Returns true if the heap is empty.

##.length A read-only length property representing the number of nodes in the heap.

Current Tags

  • 0.3.1                                ...           latest (12 years ago)

3 Versions

  • 0.3.1                                ...           12 years ago
  • 0.3.0                                ...           12 years ago
  • 0.2.0                                ...           12 years ago
Maintainers (1)
Downloads
Total 8
Today 0
This Week 0
This Month 4
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (2)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE