Skip to main content

Priority Queue in Data Structures and Algorithms

In this tutorial, we'll explore priority queues in Data Structures and Algorithms. We'll discuss what priority queues are, how they're used, and how to implement them in various programming languages.

Priority Queue in Data Structures and Algorithms​

Priority Queue

A priority queue is a data structure similar to a regular queue or stack, but each element has an associated priority. Elements with higher priorities are dequeued before elements with lower priorities. Priority queues are commonly used in scenarios where elements need to be processed based on their importance or urgency.

A priority queue supports the following operations:

  1. Insertion: Adds an element to the priority queue with its associated priority.
  2. Deletion: Removes and returns the element with the highest priority from the priority queue.
  3. Peek: Returns the element with the highest priority without removing it.
  4. isEmpty: Checks if the priority queue is empty.
  5. Size: Returns the number of elements in the priority queue.
  6. Clear: Removes all elements from the priority queue.

Additionally, priority queues may support other operations such as searching, updating priorities, merging, and iterating.

Here's an example of a priority queue in some programming languages:


class PriorityQueue {
constructor() {
this.items = [];
}

// Insertion operation
enqueue(element, priority) {
const queueElement = { element, priority };
let added = false;
for (let i = 0; i < this.items.length; i++) {
if (queueElement.priority < this.items[i].priority) {
this.items.splice(i, 0, queueElement);
added = true;
break;
}
}
if (!added) {
this.items.push(queueElement);
}
}

// Deletion operation
dequeue() {
if (this.isEmpty()) {
return "Underflow";
}
return this.items.shift().element;
}

// Peek operation
peek() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.items[0].element;
}

// isEmpty operation
isEmpty() {
return this.items.length === 0;
}

// Size operation
size() {
return this.items.length;
}

// Clear operation
clear() {
this.items = [];
}

// Print operation
print() {
console.log(this.items);
}
}

let priorityQueue = new PriorityQueue();
priorityQueue.enqueue("Task 1", 2);
priorityQueue.enqueue("Task 2", 1);
priorityQueue.enqueue("Task 3", 3);
priorityQueue.print(); // Output: [{ element: 'Task 2', priority: 1 }, { element: 'Task 1', priority: 2 }, { element: 'Task 3', priority: 3 }]
console.log(priorityQueue.dequeue()); // Output: Task 2
console.log(priorityQueue.peek()); // Output: Task 1
console.log(priorityQueue.isEmpty()); // Output: false
console.log(priorityQueue.size()); // Output: 2
priorityQueue.clear();
priorityQueue.print(); // Output: []

Conclusion​

In this tutorial, we explored priority queues in Data Structures and Algorithms. We discussed their characteristics, operations, and provided implementations in JavaScript, Python, TypeScript, C++, and Java. Priority queues are valuable for managing elements based on their priorities, enabling efficient processing of high-priority tasks. Understanding priority queues can enhance your problem-solving skills in various domains.

This comprehensive tutorial covers priority queues, including their operations and implementations in different programming languages. It serves as a valuable resource for understanding and utilizing priority queues effectively in your projects.