Priority Queue data structure A priority queue is an abstract data type where each element has a priority. Elements with higher priority are served before elements with lower priority. This implementation uses a min heap internally.
const pq = new PriorityQueue();pq.enqueue(5);pq.enqueue(3);pq.enqueue(7);pq.peek(); // 3pq.dequeue(); // 3pq.peek(); // 5 Copy
const pq = new PriorityQueue();pq.enqueue(5);pq.enqueue(3);pq.enqueue(7);pq.peek(); // 3pq.dequeue(); // 3pq.peek(); // 5
Private
Moves an element down the heap to maintain heap property
Moves an element up the heap to maintain heap property
Clears all elements from the queue
Removes and returns the element with highest priority
Adds an element to the queue
Returns true if the queue is empty
Returns the element with highest priority without removing it
Returns the number of elements in the queue
Swaps two elements in the heap
Returns an array representation of the queue
Static
Default comparator for min priority queue (smaller values have higher priority)
Generated using TypeDoc
Priority Queue data structure A priority queue is an abstract data type where each element has a priority. Elements with higher priority are served before elements with lower priority. This implementation uses a min heap internally.
Example