@datastructures-js/priority-queue
A highly performant priority queue implementation using a Heap data structure.
Table of Contents
Install
npm install --save @datastructures-js/priority-queue
API
There are two types of PriorityQueue in this repo: MinPriorityQueue which uses a MinHeap and considers an element with smaller priority number as higher in priority. And MaxPriorityQueue which uses a MaxHeap and cosiders an element with bigger priority number as higher in priority.
require
const { MinPriorityQueue, MaxPriorityQueue } = require('@datastructures-js/priority-queue');
import
import { MinPriorityQueue, MaxPriorityQueue } from '@datastructures-js/priority-queue';
Construction
Example
const patientsQueue = new MinPriorityQueue();
const biddersQueue = new MaxPriorityQueue();
.enqueue(element, priority)
adds an element with a priority (number) to the queue.
params |
---|
name | type |
element | object |
priority | number |
Example
patientsQueue.enqueue('patient y', 1);
patientsQueue.enqueue('patient z', 3);
patientsQueue.enqueue('patient w', 4);
patientsQueue.enqueue('patient x', 2);
biddersQueue.enqueue('bidder y', 1000);
biddersQueue.enqueue('bidder w', 2500);
biddersQueue.enqueue('bidder z', 3500);
biddersQueue.enqueue('bidder x', 3000);
.front()
returns the element with highest priority in the queue.
return | description |
---|
object | object literal with "priority" and "element" props |
Example
console.log(patientsQueue.front());
console.log(biddersQueue.front());
.back()
returns an element with lowest priority in the queue. If multiple elements exist at the lowest priority, the one that was inserted first will be returned.
return | description |
---|
object | object literal with "priority" and "element" props |
Example
patientsQueue.enqueue('patient m', 4);
patientsQueue.enqueue('patient c', 4);
console.log(patientsQueue.back());
biddersQueue.enqueue('bidder m', 1000);
biddersQueue.enqueue('bidder c', 1000);
console.log(biddersQueue.back());
.dequeue()
removes and returns the element with highest priority in the queue.
return | description |
---|
object | object literal with "priority" and "element" props |
Example
console.log(patientsQueue.dequeue());
console.log(patientsQueue.front());
console.log(biddersQueue.dequeue());
console.log(biddersQueue.front());
.isEmpty()
checks if the queue is empty.
Example
console.log(patientsQueue.isEmpty());
console.log(biddersQueue.isEmpty());
.size()
returns the number of elements in the queue.
Example
console.log(patientsQueue.size());
console.log(biddersQueue.size());
.toArray()
returns a sorted array of elements by their priorities from highest to lowest.
return | description |
---|
array | an array of object literals with "priority" & "element" props |
Example
console.log(patientsQueue.toArray());
console.log(biddersQueue.toArray());
.clear()
clears all elements in the queue.
Example
patientsQueue.clear();
console.log(patientsQueue.size());
console.log(patientsQueue.front());
console.log(patientsQueue.dequeue());
biddersQueue.clear();
console.log(biddersQueue.size());
console.log(biddersQueue.front());
console.log(biddersQueue.dequeue());
Build
grunt build
License
The MIT License. Full License is here