Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
The Priority Queue in Javascript
In this article, we are going to discuss the priority queue data structure in JavaScript.
A priority queue is an abstract data type (ADT) 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. If two elements have the same priority, they are served according to their order in the queue.
How Priority Queue Works
Priority queues maintain elements in sorted order based on their priority values. Lower priority numbers typically indicate higher priority (like emergency room triage - priority 1 is more urgent than priority 3).
Priority Queue Implementation
Here's a complete implementation of a priority queue class with all essential operations:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Priority Queue Implementation</title>
</head>
<body>
<div id="output"></div>
<script type="text/javascript">
class QueueElement {
constructor(elem, priority) {
this.element = elem;
this.priority = priority;
}
}
class PriorityQueue {
constructor() {
this.items = [];
}
// Add element with priority
enqueue(elem, priority) {
let queueElement = new QueueElement(elem, priority);
let added = false;
// Insert element based on priority (lower number = higher priority)
for (let i = 0; i < this.items.length; i++) {
if (this.items[i].priority > queueElement.priority) {
this.items.splice(i, 0, queueElement);
added = true;
break;
}
}
// If not added, push to end (lowest priority)
if (!added) {
this.items.push(queueElement);
}
}
// Remove and return highest priority element
dequeue() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.items.shift();
}
// Get front element without removing
front() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.items[0];
}
// Get rear element
rear() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.items[this.items.length - 1];
}
// Check if queue is empty
isEmpty() {
return this.items.length === 0;
}
// Display all elements
display() {
if (this.isEmpty()) {
return "Queue is empty";
}
let result = [];
for (let i = 0; i < this.items.length; i++) {
result.push(`${this.items[i].element} (P:${this.items[i].priority})`);
}
return result.join(" ? ");
}
}
// Create and test priority queue
let pq = new PriorityQueue();
let output = document.getElementById('output');
// Add elements with different priorities
pq.enqueue("Alice", 2);
pq.enqueue("Cullen", 4);
pq.enqueue("Edward", 1);
pq.enqueue("Bella", 2);
pq.enqueue("Jacob", 3);
output.innerHTML += "<h3>Priority Queue Operations:</h3>";
output.innerHTML += "Elements in queue: " + pq.display() + "<br><br>";
// Show front and rear elements
output.innerHTML += "Front element: " + pq.front().element + " (Priority: " + pq.front().priority + ")<br>";
output.innerHTML += "Rear element: " + pq.rear().element + " (Priority: " + pq.rear().priority + ")<br><br>";
// Dequeue operations
output.innerHTML += "<strong>Dequeue Operations:</strong><br>";
while (!pq.isEmpty()) {
let removed = pq.dequeue();
output.innerHTML += "Removed: " + removed.element + " (Priority: " + removed.priority + ")<br>";
}
</script>
</body>
</html>
Priority Queue Operations: Elements in queue: Edward (P:1) ? Alice (P:2) ? Bella (P:2) ? Jacob (P:3) ? Cullen (P:4) Front element: Edward (Priority: 1) Rear element: Cullen (Priority: 4) Dequeue Operations: Removed: Edward (Priority: 1) Removed: Alice (Priority: 2) Removed: Bella (Priority: 2) Removed: Jacob (Priority: 3) Removed: Cullen (Priority: 4)
Key Operations
The priority queue supports the following essential operations:
| Operation | Description | Time Complexity |
|---|---|---|
enqueue(element, priority) |
Adds element with given priority | O(n) |
dequeue() |
Removes and returns highest priority element | O(1) |
front() |
Returns highest priority element without removing | O(1) |
isEmpty() |
Checks if queue is empty | O(1) |
Common Use Cases
Priority queues are widely used in:
- Task scheduling: Operating systems use priority queues to schedule processes
- Dijkstra's algorithm: Finding shortest paths in graphs
- Huffman coding: Data compression algorithms
- Emergency systems: Hospital triage, emergency response
- A* pathfinding: Game development and robotics
Conclusion
Priority queues provide an efficient way to manage elements based on importance rather than insertion order. This implementation uses an array with linear insertion, making it suitable for small to medium-sized datasets where priority-based processing is essential.
