a performant priority queue implementation using a Heap data structure.
Package Exports
@datastructures-js/priority-queue
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@datastructures-js/priority-queue) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@datastructures-js/priority-queue
A performant priority queue implementation using a Heap data structure.
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.
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
// the priority not part of the enqueued elementconst patientsQueue =newMinPriorityQueue();// the priority is a prop of the queued elementconst biddersQueue =newMaxPriorityQueue({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
runtime
O(log(n))
Example
// MinPriorityQueue Example, where priority is the turn for example
patientsQueue.enqueue('patient y',1);// highest priority
patientsQueue.enqueue('patient z',3);
patientsQueue.enqueue('patient w',4);// lowest priority
patientsQueue.enqueue('patient x',2);// MaxPriorityQueue Example, where priority is the bid for example. Priority is obtained from the callback.
biddersQueue.enqueue({name:'bidder y',value:1000});// lowest priority
biddersQueue.enqueue({name:'bidder w',value:2500});
biddersQueue.enqueue({name:'bidder z',value:3500});// highest priority
biddersQueue.enqueue({name:'bidder x',value:3000});
.front()
returns the element with highest priority in the queue.
return
description
object
object literal with "priority" and "element" props
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