Priority queue(优先队列) implementation in JavaScript
npm i @kartjim/priority-queue
import PriorityQueue from '@kartjim/priority-queue';
or use CDN:
<script src="https://cdn.jsdelivr.net/npm/@kartjim/[email protected]/priorityqueue.min.js"></script>
const data = [];
for (let i = 0; i < 50; i++) {
data.push(~~(50 * Math.random()));
}
const sorted = data.slice().sort((a, b) => a - b);
const heap = new PriorityQueue();
for (let i = 0; i < data.length; i++) {
heap.push(data[i]);
}
heap.peek() === sorted[0] // true
let ans = [];
while (!heap.isEmpty()) {
ans.push(heap.pop());
}
ans == sorted // deeply equal
- constructor
constructor(data?: T[],compare?: (a: T, b: T) => number);
push(val: T) : void;
- push an element into PriorityQueue
peek() : T | undefined;
- get the top element
pop() : T | undefined;
- pop an element, and return it
isEmpty() : boolean;
- check if the PriorityQueue is empty