node-priority-queue
Install
npm install node-priority-queue
Description
node-priority-queue lets you create a priority queue(backed by heap). You can specify the property of object to set priority on to create either max-queue or min-queue.
Operations
- Get priority queue
- Peek
- Pop
Example 1
const node_priority = require('node-priority-queue');
Create an empty queue
node_priority.createEmptyQueue();
Add elements
node_priority.addElement({a : 100});
node_priority.addElement({a : -10});
node_priority.addElement({a : 13});
Property of object to set priority on (1 = ascending, -1 = descending)
node_priority.setPropertyToPrioritize("a", -1);
Get the priority queue
var queue = node_priority.getQueue();
Peek
var item = node_priority.peek();
Pop
var item = node_priority.pop();
If you pop an item from queue, then you need to call node_priority.getQueue()
to get updated queue
Example 2
var array = []; array.push({'a' : 1}); array.push({'a' : 4}); array.push({'a' : 2}); array.push({'a' : 3}); node_priority.setInputArray(array); node_priority.setPropertyToPrioritize('a', 1); var queue = node_priority.getQueue(); console.log(queue); queue.push({a : 1000}); queue.push({a : -19}); queue.push({a : 121}); queue = node_priority.getQueue(queue); console.log(queue);