JavaScript Priority Queue Implementation
A priority queue is an abstract data type where each element has a "priority" associated with it. Elements are served in order of their priority. This is incredibly useful in various algorithms, such as Dijkstra's shortest path, Huffman coding, and task scheduling. Your task is to implement a functional priority queue in JavaScript.
Problem Description
You need to create a JavaScript class named PriorityQueue that simulates a priority queue. The priority queue should support the following operations:
enqueue(element, priority): Adds anelementwith a givenpriorityto the queue. Lower numerical values represent higher priority.dequeue(): Removes and returns the element with the highest priority. If the queue is empty, it should returnnull.front(): Returns the element with the highest priority without removing it. If the queue is empty, it should returnnull.isEmpty(): Returnstrueif the queue is empty,falseotherwise.size(): Returns the number of elements in the queue.
Key Requirements:
- The priority queue should store elements along with their associated priorities.
dequeuemust always return the element with the smallest priority value.- If multiple elements have the same highest priority, any one of them can be dequeued. The order among equal-priority elements doesn't strictly matter for this challenge, but a consistent behavior is preferred.
- The implementation should be efficient.
Expected Behavior:
When elements are enqueued, they should be internally organized such that the element with the lowest priority value is readily accessible. dequeue and front operations should always reflect this highest priority element.
Edge Cases to Consider:
- Enqueuing elements into an empty queue.
- Dequeuing from an empty queue.
- Accessing the front of an empty queue.
- Handling multiple elements with the same highest priority.
Examples
Example 1:
const pq = new PriorityQueue();
pq.enqueue("Task A", 3);
pq.enqueue("Task B", 1);
pq.enqueue("Task C", 2);
console.log(pq.dequeue()); // Output: "Task B"
console.log(pq.front()); // Output: "Task C"
console.log(pq.size()); // Output: 2
Explanation: "Task B" has the highest priority (1). After dequeuing, "Task C" (priority 2) becomes the highest priority element.
Example 2:
const pq = new PriorityQueue();
pq.enqueue("Low Prio", 10);
pq.enqueue("High Prio", 0);
pq.enqueue("Mid Prio", 5);
pq.enqueue("Another High Prio", 0);
console.log(pq.dequeue()); // Output: "High Prio" (or "Another High Prio")
console.log(pq.dequeue()); // Output: "Another High Prio" (or "High Prio")
console.log(pq.isEmpty()); // Output: false
console.log(pq.dequeue()); // Output: "Mid Prio"
console.log(pq.dequeue()); // Output: "Low Prio"
console.log(pq.isEmpty()); // Output: true
console.log(pq.dequeue()); // Output: null
Explanation: Elements with the same highest priority (0) are handled. The order of dequeuing them might vary, but both will be removed before elements with lower priorities.
Example 3:
const pq = new PriorityQueue();
console.log(pq.isEmpty()); // Output: true
console.log(pq.size()); // Output: 0
console.log(pq.front()); // Output: null
console.log(pq.dequeue()); // Output: null
Explanation: Demonstrates the behavior of an empty priority queue.
Constraints
- The number of elements in the priority queue can range from 0 to 100,000.
- Priorities will be non-negative integers.
- Elements can be of any JavaScript type (strings, numbers, objects, etc.).
- The
enqueue,dequeue, andfrontoperations should ideally have a time complexity of O(log n) or better, where n is the number of elements in the queue.isEmptyandsizeshould be O(1).
Notes
- Consider using a data structure that allows for efficient insertion and retrieval of the minimum element. A Min-Heap is a common and effective choice for implementing priority queues.
- You'll need to manage both the element and its priority together. A simple way to do this is by storing them as an object or an array within your queue's internal structure.
- Think about how to maintain the heap property efficiently after insertions and deletions.