min-priority-queue-typed
v2.6.0
Published
Min Priority Queue
Maintainers
Keywords
Readme
What
Brief
This is a standalone Min Priority Queue data structure from the data-structure-typed collection. If you wish to access more data structures or advanced features, you can transition to directly installing the complete data-structure-typed package
How
install
npm
npm i min-priority-queue-typed --saveyarn
yarn add min-priority-queue-typedsnippet
Shortest job first scheduling
const jobs = new MinPriorityQueue<number>();
jobs.add(8); // 8 seconds
jobs.add(2); // 2 seconds
jobs.add(5); // 5 seconds
jobs.add(1); // 1 second
// Shortest job first
console.log(jobs.poll()); // 1;
console.log(jobs.poll()); // 2;
console.log(jobs.poll()); // 5;
console.log(jobs.poll()); // 8;Event-driven simulation with timestamps
interface Event {
time: number;
action: string;
}
const timeline = new MinPriorityQueue<Event>([], {
comparator: (a, b) => a.time - b.time
});
timeline.add({ time: 300, action: 'Timeout' });
timeline.add({ time: 100, action: 'Request received' });
timeline.add({ time: 200, action: 'Processing done' });
timeline.add({ time: 150, action: 'Cache hit' });
const order = [];
while (timeline.size > 0) {
order.push(timeline.poll()!.action);
}
console.log(order); // [
// 'Request received',
// 'Cache hit',
// 'Processing done',
// 'Timeout'
// ];Huffman coding frequency selection
// Character frequencies for Huffman tree building
const freq = new MinPriorityQueue<[number, string]>([], {
comparator: (a, b) => a[0] - b[0]
});
freq.add([5, 'a']);
freq.add([9, 'b']);
freq.add([12, 'c']);
freq.add([2, 'd']);
// Always pick two lowest frequencies
const first = freq.poll()!;
const second = freq.poll()!;
console.log(first[1]); // 'd'; // freq 2
console.log(second[1]); // 'a'; // freq 5
// Combined node goes back
freq.add([first[0] + second[0], first[1] + second[1]]);
console.log(freq.peek()![0]); // 7;API docs & Examples
Examples Repository
