Notoriously Psychedelic Modules
    Have ideas to improve npm?Join in the discussion! »

    javascript-priority-queue
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.0 • Public • Published

    JavaScript implementation of Priority Queue

    Min Heap or Max Heap

    Install

    yarn add javascript-priority-queue or npm install javascript-priority-queue

    Usage

    // Include using "import"
    import PriorityQueue from 'javascript-priority-queue';
    const minQueue = new PriorityQueue('min'); // Lowest priority first
    const maxQueue = new PriorityQueue('max'); // Highest priority first
     
    // Include using "require"
    const PriorityQueue = require('javascript-priority-queue');
    const minQueue = new PriorityQueue.default('min'); // Lowest priority first
    const maxQueue = new PriorityQueue.default('max'); // Highest priority first
     
    // Functions
    queue.enqueue(SOME_ITEM, PRIORITY_NUMBER); // add to the queue. e.g. queue.enqueue("a", 1)
    queue.dequeue(); // returns the current first item and remove it from the queue.
    queue.peek(); // returns the current first item
    queue.size(); // returns the number of items in the queue
    queue.toString(); // NOT IN SORTED ORDER! prints the all items

    Example

    const PriorityQueue = require('javascript-priority-queue');
     
    const queue = new PriorityQueue.default('min');
     
    queue.enqueue('a', 2);
    queue.enqueue('b', 1);
    queue.enqueue('c', -0.156);
    queue.enqueue('d', 351680468468489);
    queue.enqueue('e', 50);
     
    console.log(queue.toString());
    /* output: (NOT IN SORTED ORDER)
    Value: c  Priority: -0.156
    Value: a  Priority: 2
    Value: b  Priority: 1
    Value: d  Priority: 351680468468489
    Value: e  Priority: 50
    */
     
    console.log(queue.dequeue());
    // output: c
     
    console.log(queue.peek());
    // output: b
     
    console.log(queue.size());
    // output: 4

    Install

    npm i javascript-priority-queue

    DownloadsWeekly Downloads

    20

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    22.4 kB

    Total Files

    15

    Last publish

    Collaborators

    • avatar