@datastructures-js/priority-queue
A highly performant priority queue implementation using a Min Heap data structure.
Table of Contents
Install
npm install --save @datastructures-js/priority-queue
API
require
const PriorityQueue = require('@datastructures-js/priority-queue');
import
import PriorityQueue from '@datastructures-js/priority-queue';
Create a Priority Queue
const priorityQueue = new PriorityQueue();
.enqueue(element, priority)
adds an element with a priority (number) to the queue. The smaller the number, the higher the priority.
runtime | params |
---|
O(log(n)) |
element: object
priority: number
|
priorityQueue.enqueue('patient y', 1);
priorityQueue.enqueue('patient z', 3);
priorityQueue.enqueue('patient w', 4);
priorityQueue.enqueue('patient x', 2);
.front()
returns the element with highest priority in the queue.
console.log(priorityQueue.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.
priorityQueue.enqueue('patient m', 4);
priorityQueue.enqueue('patient c', 4);
console.log(priorityQueue.back());
.dequeue()
removes and returns the element with highest priority in the queue.
runtime | return |
---|
O(log(n)) | object |
console.log(priorityQueue.dequeue());
console.log(priorityQueue.front());
.isEmpty()
checks if the queue is empty.
runtime | return |
---|
O(1) | {boolean} |
console.log(priorityQueue.isEmpty());
.size()
returns the number of elements in the queue.
console.log(priorityQueue.size());
.toArray()
returns an sorted array of elements from highest priority to lowest.
runtime | return |
---|
O(n*log(n)) | array |
console.log(priorityQueue.toArray());
.clear()
clears all elements in the queue.
priorityQueue.clear();
console.log(priorityQueue.size());
console.log(priorityQueue.front());
console.log(priorityQueue.dequeue());
Build
grunt build
License
The MIT License. Full License is here