priorityqueue
An implementation of Priority Queue
Last updated 10 months ago by berlysia .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install priorityqueue 
SYNC missed versions from official npm registry.

PriorityQueue

An implementation of priority queue in javascript.

Installation

npm install priorityqueue

Example

import PriorityQueue from "priorityqueue";

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}

const numericCompare = (a, b) => (a > b ? 1 : a < b ? -1 : 0);

const comparator = (a, b) => {
  const x = numericCompare(a.x, b.x);
  const y = numericCompare(a.y, b.y);
  return x ? x : y;
};

const pq = new PriorityQueue({ comparator });

pq.push(new Point(4, 6));
pq.push(new Point(2, 3));
pq.push(new Point(5, 1));
pq.push(new Point(1, 2));
console.log(pq.pop()); // => {x: 5, y: 1}
console.log(pq.top()); // => {x: 4, y: 6}
pq.push(new Point(3, 4));
pq.push(new Point(6, 5));
console.log(pq.length); // => 5
console.log(pq.top()); // => {x: 6, y: 5}

Options

PriorityQueue(options = {})

options.comparator

options.comparator defines an order of each values in PriorityQueue.

A comparator function format is in according with an argument of Array.prototype.sort.

⚠️ The predefined order is also the same as Array.prototype.sort.

Variation

BinaryHeap(default)

Binary heap is a simple and efficient in almost cases.

cons:

  • slow with large amount of items(over 10k)
  • slow in merge operation especially

PairingHeap

pros:

  • super fast in merge operation(constant time)

SkewHeap

pros:

  • super fast in merge operation(constant time)

Not to use:

  • with sequence completely sorted

Import specific implementation

import PriorityQueue, {
  BinaryHeap,
  PairingHeap,
  SkewHeap,
} from "priorityqueue";

console.log(PriorityQueue === BinaryHeap); // => true

Current Tags

  • 1.0.0                                ...           latest (10 months ago)
  • 1.0.0-rc3.0                                ...           next (10 months ago)

12 Versions

  • 1.0.0                                ...           10 months ago
  • 1.0.0-rc3.0                                ...           10 months ago
  • 1.0.0-rc3                                ...           a year ago
  • 1.0.0-rc2                                ...           a year ago
  • 1.0.0-rc1                                ...           2 years ago
  • 0.2.1                                ...           2 years ago
  • 0.2.0                                ...           4 years ago
  • 0.1.0                                ...           5 years ago
  • 0.0.4                                ...           5 years ago
  • 0.0.3                                ...           5 years ago
  • 0.0.2                                ...           5 years ago
  • 0.0.1                                ...           5 years ago

Copyright 2014 - 2016 © taobao.org |