Back to Data Structures & Algorithms
2025-12-226 min read

Priority Queue Implementations in Python, Java, C, and C++

Learn Priority Queue Implementations in Python, Java, C, and C++ step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on Priority Queue Implementations! In this tutorial, we will delve deep into various implementations of priority queues using Python, Java, C, and C++. Understanding these techniques will not only help you grasp the practical applications but also prepare you for real-world coding challenges.

Why This Matters

Priority queues are essential in computer science as they provide efficient solutions for problems that require frequent insertion and deletion of elements while maintaining a specific order. Priority queues play a crucial role in scheduling algorithms, network routing, dijkstra's shortest path algorithm, and many other areas. Mastering priority queue implementations can help you solve complex real-world bugs and excel in coding interviews.

Prerequisites

To follow this tutorial, you should be familiar with the following concepts:

  1. Basic understanding of data structures (arrays, linked lists, stacks, queues)
  2. Familiarity with programming constructs like loops, functions, and conditional statements in Python, Java, C, and C++
  3. Understanding of Big O notation
  4. Knowledge of heap data structure (optional but recommended for a deeper understanding)

Core Concept

Definition

A priority queue is a collection of elements where each element has a priority associated with it. The elements with higher priorities are processed before the ones with lower priorities. Priority queues can be implemented using various data structures, such as heaps and arrays.

Heap-Based Priority Queue (Python and Java)

In Python and Java, we can implement a priority queue using a heap. A heap is a complete binary tree that satisfies the heap property: if A is a parent node, then its child nodes B and C must satisfy either A > B or A <= B. In a max-heap, the root always has the highest priority, while in a min-heap, the root has the lowest priority.

import heapq

def create_max_priority_queue():
return []

def create_min_priority_queue():
return []

def insert(priority_queue, element, max_heap=True):
if max_heap:
heapq.heappush(priority_queue, -element)
else:
heapq.heappush(priority_queue, element)

def remove(priority_queue, max_heap=True):
return -heapq.heappop(priority_queue) if max_heap else heapq.heappop(priority_queue)

def peek(priority_queue, max_heap=True):
return -priority_queue[0] if max_heap else priority_queue[0]

Heap-Based Priority Queue (Java)

In Java, we can use the PriorityQueue class which is a ready-to-use min-heap implementation. However, it's important to understand the underlying principles of heap-based priority queues.

import java.util.PriorityQueue;

public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue<Integer> maxPQ = new PriorityQueue<>((n1, n2) -> n2 - n1); // Max-heap implementation
PriorityQueue<Integer> minPQ = new PriorityQueue<>(); // Min-heap implementation (default is min-heap)

// Insert elements and manipulate the priority queues
}
}

Array-Based Priority Queue (C and C++)

In C and C++, we can implement a priority queue using an array. The array stores the elements, while an additional array or linked list is used to store the priorities. We'll focus on a min-priority queue implementation in this tutorial.

#include <stdio.h>
#include <stdlib.h>

typedef struct PriorityQueue {
int *elements;
int *priorities;
int size;
int capacity;
} PriorityQueue;

PriorityQueue* create_priority_queue(int capacity) {
PriorityQueue* pq = (PriorityQueue*)malloc(sizeof(PriorityQueue));
pq->elements = (int*)malloc(capacity * sizeof(int));
pq->priorities = (int*)malloc(capacity * sizeof(int));
pq->size = 0;
pq->capacity = capacity;
return pq;
}

void insert(PriorityQueue* pq, int element, int priority) {
if (pq->size == pq->capacity) {
printf("The priority queue is full.\n");
return;
}
pq->elements[pq->size] = element;
pq->priorities[pq->size] = priority;
int i = pq->size - 1;
while (i && pq->priorities[i] > pq->priorities[parent(i)]) {
swap(&pq->elements[i], &pq->elements[parent(i)]);
swap(&pq->priorities[i], &pq->priorities[parent(i)]);
i = parent(i);
}
pq->size++;
}

int parent(int index) {
return (index - 1) / 2;
}

void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}

Worked Example

In this example, we'll implement a priority queue using a max-heap in Python and an array-based min-priority queue in C++. We'll insert elements with their corresponding priorities and then remove the highest-priority (maximum in Python) and lowest-priority (minimum in C++) elements.

Python implementation of a priority queue using max-heap

pq = create_max_priority_queue()

insert(pq, 3, 10)

insert(pq, 2, 5)

insert(pq, 1, 20)

print("Peek:", peek(pq)) # Output: Peek: 20

print("Remove:", remove(pq)) # Output: Remove: 20

print("Peek:", peek(pq)) # Output: Peek: 10

C++ implementation of a priority queue using array-based min-priority queue

PriorityQueue* pq = create_priority_queue(5);

insert(pq, 3, 2);

insert(pq, 2, 1);

insert(pq, 1, 3);

printf("Peek: %d\n", peek(pq)->elements[0]); // Output: Peek: 1

remove_min(pq);

printf("Remove Min: %d\n", remove_min(pq)); // Output: Remove Min: 2

printf("Peek: %d\n", peek(pq)->elements[0]); // Output: Peek: 3

Common Mistakes

  1. Forgetting to update the heap property after inserting or removing an element (Python and Java)
  2. Accessing out-of-bounds array elements when manipulating the priority queue (C and C++)
  3. Using a max-heap where a min-heap is required, or vice versa (Python and Java)
  4. Failing to allocate enough memory for the priority queue in C and C++
  5. Not properly implementing the heapify function when using an array-based implementation (C and C++)
  6. Implementing custom heap functions without considering edge cases like empty queues or full queues
  7. Misunderstanding the difference between a max-heap and a min-heap, leading to incorrect implementations or usage

Practice Questions

  1. Implement a min-priority queue using a max-heap in Python or Java.
  2. Write a function to check if a given array is a valid min-heap.
  3. Implement a priority queue using a binary search tree in C++.
  4. Given a list of tasks with their deadlines and priorities, schedule the tasks so that the most important task with the earliest deadline is always processed first.
  5. Write an efficient algorithm to find the kth smallest element in a min-priority queue.
  6. Implement a custom heap function for inserting elements into a priority queue using an array-based implementation (C and C++).
  7. Extend the array-based min-priority queue implementation to support deletion of an element with a specific priority.
  8. Compare the time complexity of various priority queue implementations in different programming languages.
  9. Implement a priority queue that supports both insertion and deletion of elements with the same priority (tie-breaking policy).
  10. Write a function to merge two priority queues into one while maintaining the order of elements.

FAQ

What is the time complexity of inserting an element into a priority queue?

  • For heap-based implementations, it's O(log n) in both Python and Java.
  • For array-based implementations, it's O(n) for the worst case (when adding at the end), but usually O(log n) in practice due to heapify operations.

What is the time complexity of removing an element from a priority queue?

  • For heap-based implementations, it's O(log n) in both Python and Java.
  • For array-based implementations, it's O(log n) on average due to heapify operations, but O(n) for the worst case (when removing the root).

Can we use a linked list instead of an array for array-based priority queues?

Yes, we can implement array-based priority queues using linked lists as well. However, it may not be as efficient due to slower access times compared to arrays.

What is the difference between a min-heap and a max-heap?

A min-heap has its root element with the smallest value (minimum), while a max-heap has its root element with the largest value (maximum).

Why do we use heapq instead of implementing our own heap in Python?

Heapq is an optimized implementation of heaps in Python that provides faster performance and handles edge cases more efficiently than a custom implementation.

How can I implement a priority queue that supports dynamic resizing of the array-based implementation?

You can dynamically allocate memory for the array when the current capacity is reached, and then rebuild the heap to maintain its properties.

Can we use a binary search tree for implementing a priority queue?

Yes, a binary search tree can be used to implement a priority queue. However, it may not provide the same time complexity guarantees as heaps or array-based implementations for common operations like insertion and deletion.

Priority Queue Implementations in Python, Java, C, and C++ | Data Structures & Algorithms | XQA Learn