A small and mighty suite of data structures in JavaScript.


Keywords
javascript, priority, queue, binary heap, disjoint sets, union find, data structures, algorithms, array, binary-heap, data-structures, disjoint-sets, priority-queue, union-find
License
MIT
Install
npm install lytepq@1.1.9

Documentation

LytePQ ยท npm version license PRs welcome

A small and mighty priority queue library in JavaScript.

Servicing in parallel with LyteSets (under maintenance), a JavaScript disjoint sets library.

Install

Install with either Yarn or NPM via yarn add lytepq or npm install lytepq.

LytePQ

Perks

โœ… Packed with all basic priority queue operations.
๐Ÿš€ Unopinionated functionality exposure - you decide how to use LytePQ.
๐Ÿ’ป Perfect for competitive programming, online tests, interviews, etc. Dijkstra's in JS made easy!
๐Ÿ“” Comprehensive JSDoc annotations and intellisense.
๐Ÿ”ญ TypeScript support.

Getting Started

Import into your project in the following ways.

import { LytePQ } from "lytepq"; // ES

const { LytePQ } = require("lytepq"); // CommonJS

Demo

// LytePQ is a min priority queue by default
const minQ = new LytePQ();
pq.push(2), pq.push(1), pq.push(3);
const smallest = pq.peek(); // returns the smallest item - 1

// creates a max priority queue with a custom comparator function
const maxQ = new LytePQ([2, 3, -1], (a, b) => b - a);
const largest = pq.pop(); // removes and returns the largest item - 3

// queue items can be objects with a matching comparator function
const objectQ = new LytePQ(
  [
    [0, 8],
    [1, 2],
    [2, 7],
  ],
  (a, b) => a[1] - b[1]
);
const smallestObj = objectQ.pop(); // [1, 2]

Advanced Use Cases

const minQ = new LytePQ();
pq.push(2), pq.push(1), pq.push(3);
// removes and returns the first instance of the specified item
const specified = pq.pop(2); // 2

Contributing

Issues and pull requests are welcome! Contact me on Twitter if needed.

Credits

This library took inspiration from Vladimir Agafonkin's tinyqueue.