A priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority.
While priority queues are often implemented with heaps, they are conceptually distinct from heaps. A priority queue is a concept like "a list" or "a map"; just as a list can be implemented with a linked list or an array, a priority queue can be implemented with a heap or a variety of other methods such as an unordered array.
Install with npm
npm i javascript-algo-ds
Import in your javascript file as:
const PriorityQueue = require('javascript-algo-ds/src/data-structure/priority-queue/priorityQueue')
const queue = new PriorityQueue()
- heap - array of numbers in ordered way
- size - number of elements in the heap
- priorities - a map which contains priority of each item
- queue.enqueue(value, [priority])
- queue.dequeue()
- queue.peek()
- queue.add(value)
- queue.has(value)
- queue.toString()
- value - number
- value to be added
- priority - number
- Default value - 0
- Priority of
value
- Optional
Returns this
Returns number
Returns the element at front of the queue.
Type - number | null
Adds value
to the queue with priority 0.
- value - number
- The value to be added.
Returns this
Checks if value
is contained in the queue or not.
- value - number
- The number to be checked.
Type - boolean
Converts the heap in array and returns it
Type - String
@static
Creates the queue from an array
- array - number[][]
- 2-D array containing elements to be added in first column and their priorities in second column
Type - PriorityQueue