@datastructures-js/priority-queue
A 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
The constructor can accept a callback to get the priority from the queued element. If not passed, the priortiy should be passed with .enqueue
.
Example
const patientsQueue = new MinPriorityQueue();
const biddersQueue = new MaxPriorityQueue({ priority: (bid) => bid.value });
.enqueue(element[, priority])
adds an element with a priority (number) to the queue. Priority is not required here if a priority callback has been defined in the constructor. If passed here in addition to an existing constructor callback, it will override the callback one.
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({ name: 'bidder y', value: 1000 });
biddersQueue.enqueue({ name: 'bidder w', value: 2500 });
biddersQueue.enqueue({ name: 'bidder z', value: 3500 });
biddersQueue.enqueue({ name: 'bidder x', value: 3000 });
from(entries)
Creates a priority queue from an array of element-priority pairs.
MaxPriorityQueue.from([
['Alice', 19],
['Bob', 18],
['Charles', 20]
])
MinPriorityQueue.from([
['Alice', 19],
['Bob', 18],
['Charles', 20]
])
.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({ name: 'bidder m', value: 1000 });
biddersQueue.enqueue({ name: 'bidder c', value: 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