@datastructures-js/priority-queue
A performant priority queue implementation using a Heap data structure.
Contents
Install
npm install --save @datastructures-js/priority-queue
API
PriorityQueue in this repo is implemented as 3 types:
- PriorityQueue that accepts a custom comparator between elements.
- MinPriorityQueue which considers an element with smaller priority number as higher in priority.
- MaxPriorityQueue which cosiders an element with bigger priority number as higher in priority.
require
const {
PriorityQueue,
MinPriorityQueue,
MaxPriorityQueue
} = require('@datastructures-js/priority-queue');
import
import {
PriorityQueue,
MinPriorityQueue,
MaxPriorityQueue,
PriorityQueueOptions,
PriorityQueueItem
} from '@datastructures-js/priority-queue';
constructor
PriorityQueue
The constructor requires a compare callback to compare between queue elements. compare works similar to javascript sort callback: returning a number less or equal 0, means do not swap.
JS
const employeesQueue = new PriorityQueue({
compare: (e1, e2) => {
if (e1.salary > e2.salary) return -1;
if (e1.salary < e2.salary) return 1;
return e1.rank < e2.rank ? 1 : -1;
}
});
TS
interface Employee {
name: string;
salary: number;
rank: number;
}
const employeesQueue = new PriorityQueue<Employee>({
compare: (e1: Employee, e2: Employee): number => {
if (e1.salary > e2.salary) return -1;
if (e1.salary < e2.salary) return 1;
return e1.rank < e2.rank ? 1 : -1;
}
});
MinPriorityQueue/MaxPriorityQueue
The constructor accepts a priority callback option to get the numeric priority from the queued element. If not passed, the constructor adds a default priority callback that returns the numeric value of the element itself. Use this queue type when the priority is a known value and does not require complex comparison.
JS
const numbersQueue = new MinPriorityQueue();
const patientsQueue = new MinPriorityQueue();
const biddersQueue = new MaxPriorityQueue({ priority: (bid) => bid.value });
TS
const numbersQueue = new MinPriorityQueue<number>();
const patientsQueue = new MinPriorityQueue<string>();
interface Bid {
name: string;
value: number;
}
const biddersQueue = new MaxPriorityQueue<Bid>({
priority: (bid: Bid) => bid.value
});
.enqueue
PriorityQueue - .enqueue(element)
adds an element based on its comparison with other elements in the queue.
params | return | runtime |
---|
element: T | PriorityQueue<T> | O(log(n)) |
employeesQueue
.enqueue({ name: 'employee 1', salary: 2000, rank: 1 })
.enqueue({ name: 'employee 2', salary: 1500, rank: 0 })
.enqueue({ name: 'employee 3', salary: 4000, rank: 4 })
.enqueue({ name: 'employee 4', salary: 2000, rank: 2 })
.enqueue({ name: 'employee 5', salary: 3000, rank: 3 });
MinPriorityQueue/MaxPriorityQueue - .enqueue(element[, priority])
adds an element with a numeric priority to the queue. Priority is not required here if a priority callback has been provided in the constructor. If passed here with a constructor callback, it will override the callback.
params | return | runtime |
---|
element: T
priority: number
| MinPriorityQueue<T> | MaxPriorityQueue<T> | O(log(n)) |
numbersQueue
.enqueue(10)
.enqueue(-7)
.enqueue(2)
.enqueue(-1)
.enqueue(-17)
.enqueue(33);
patientsQueue
.enqueue('patient y', 1)
.enqueue('patient z', 3)
.enqueue('patient w', 4)
.enqueue('patient x', 2);
biddersQueue
.enqueue({ name: 'bidder y', value: 1000 })
.enqueue({ name: 'bidder w', value: 2500 })
.enqueue({ name: 'bidder z', value: 3500 })
.enqueue({ name: 'bidder x', value: 3000 });
.front()
returns the element with highest priority in the queue.
PriorityQueue
console.log(employeesQueue.dequeue());
MinPriorityQueue/MaxPriorityQueue
return | runtime |
---|
PriorityQueueItem<T> | O(1) |
console.log(numbersQueue.front());
console.log(patientsQueue.front());
console.log(biddersQueue.front());
.back()
returns an element with a lowest priority in the queue.
PriorityQueue
console.log(employeesQueue.back());
MinPriorityQueue/MaxPriorityQueue
return | runtime |
---|
PriorityQueueItem<T> | O(1) |
console.log(numbersQueue.back());
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.
PriorityQueue
console.log(employeesQueue.dequeue());
console.log(employeesQueue.dequeue());
console.log(employeesQueue.dequeue());
console.log(employeesQueue.dequeue());
console.log(employeesQueue.dequeue());
MinPriorityQueue/MaxPriorityQueue
return | runtime |
---|
PriorityQueueItem<T> | O(log(n)) |
console.log(numbersQueue.dequeue());
console.log(numbersQueue.front());
console.log(patientsQueue.dequeue());
console.log(patientsQueue.front());
console.log(biddersQueue.dequeue());
console.log(biddersQueue.front());
.isEmpty()
checks if the queue is empty.
console.log(numbersQueue.isEmpty());
console.log(patientsQueue.isEmpty());
console.log(biddersQueue.isEmpty());
.size()
returns the number of elements in the queue.
console.log(numbersQueue.size());
console.log(patientsQueue.size());
console.log(biddersQueue.size());
.toArray()
returns a sorted array of elements by their priorities from highest to lowest.
PriorityQueue
return | runtime |
---|
T[] | O(n*log(n)) |
console.log(employeesQueue.toArray());
MinPriorityQueue/MaxPriorityQueue
return | runtime |
---|
PriorityQueueItem<T>[] | O(n*log(n)) |
console.log(numbersQueue.toArray());
console.log(patientsQueue.toArray());
console.log(biddersQueue.toArray());
.clear()
clears all elements in the queue.
numbersQueue.clear();
console.log(numbersQueue.size());
console.log(numbersQueue.front());
console.log(numbersQueue.dequeue());
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