Heaps & Priority Queues Min Heap Max Heap & Heap Sort
Heaps and priority queues are fundamental data structures in computer science, widely used in algorithms that require quick access to the minimum or maximum element. In this tutorial, we will explore min-heaps, max-heaps, and heap sort, along with practical examples in Python that are relevant for Pakistani students learning programming and data structures.
Understanding heaps is essential if you want to efficiently solve problems like task scheduling, finding the top scores in a dataset, or managing real-world resources like hospital beds or delivery priorities in cities like Lahore, Karachi, and Islamabad. By mastering heaps and priority queues, you will gain a powerful tool for competitive programming, software development, and data analysis.
Prerequisites
Before diving into heaps, ensure you are familiar with:
- Basic Python programming (or any preferred language)
- Arrays and lists
- Binary trees concepts (nodes, children, parent relationships)
- Loops and conditional statements
- Basic algorithm analysis (time and space complexity)
Having this foundation will make it easier to understand how heaps are implemented and used in practice.
Core Concepts & Explanation
Heap Data Structure Overview
A heap is a complete binary tree that satisfies the heap property:
- Min-Heap: Every parent node is smaller than its children. The smallest element is always at the root.
- Max-Heap: Every parent node is larger than its children. The largest element is always at the root.
Heaps are commonly implemented using arrays because a complete binary tree can be efficiently stored without pointers. For a node at index i:
- Left child index =
2*i + 1 - Right child index =
2*i + 2 - Parent index =
(i-1) // 2
Example: Min-Heap array representation
Heap Tree: Array:
10 [10, 20, 15, 30, 40]
/ \
20 15
/ \
30 40
Priority Queue Concept
A priority queue is an abstract data type that always retrieves the highest-priority element first:
- In a min-priority queue, the element with the smallest value is served first.
- In a max-priority queue, the element with the largest value is served first.
Heaps are the ideal underlying data structure for priority queues because they allow efficient insertion and extraction in O(log n) time.
Heap Operations: Insert & Extract

Insert (Sift-Up / Bubble-Up):
- Add the new element at the end of the array (maintains complete tree property).
- Compare with parent; if heap property is violated, swap and repeat until fixed.
Extract-Min / Extract-Max (Sift-Down / Bubble-Down):
- Replace the root with the last element.
- Compare with children; swap with the smaller (min-heap) or larger (max-heap) child until heap property is restored.
Practical Code Examples
Example 1: Implementing a Min-Heap in Python
class MinHeap:
def __init__(self):
self.heap = []
def insert(self, val):
# Step 1: Add value to the end
self.heap.append(val)
# Step 2: Fix heap property by sifting up
self._sift_up(len(self.heap) - 1)
def _sift_up(self, index):
parent = (index - 1) // 2
if index > 0 and self.heap[index] < self.heap[parent]:
# Swap with parent if smaller
self.heap[index], self.heap[parent] = self.heap[parent], self.heap[index]
self._sift_up(parent)
def extract_min(self):
if len(self.heap) == 0:
return None
# Swap root with last element
min_val = self.heap[0]
self.heap[0] = self.heap.pop()
self._sift_down(0)
return min_val
def _sift_down(self, index):
left = 2 * index + 1
right = 2 * index + 2
smallest = index
if left < len(self.heap) and self.heap[left] < self.heap[smallest]:
smallest = left
if right < len(self.heap) and self.heap[right] < self.heap[smallest]:
smallest = right
if smallest != index:
self.heap[index], self.heap[smallest] = self.heap[smallest], self.heap[index]
self._sift_down(smallest)
# Usage Example
heap = MinHeap()
heap.insert(40)
heap.insert(20)
heap.insert(15)
heap.insert(30)
heap.insert(10)
print(heap.extract_min()) # Output: 10
Explanation:
- Each method has a clear purpose:
insertmaintains the heap while adding elements;extract_minremoves the smallest value efficiently. _sift_upand_sift_downmaintain the heap property.
Example 2: Real-World Application — Priority Queue for Pakistani Bank Transactions
Imagine Ali in Karachi needs to process bank transactions based on priority (smaller amounts first for faster processing).
import heapq
# Priority queue using heapq (min-heap)
transactions = [(5000, 'Ali'), (2000, 'Fatima'), (7000, 'Ahmad'), (3000, 'Sara')]
heapq.heapify(transactions)
# Process transactions by smallest amount first
while transactions:
amount, name = heapq.heappop(transactions)
print(f"Processing PKR {amount} for {name}")

Explanation:
heapq.heapifytransforms the list into a heap in O(n) time.heappopalways gives the smallest transaction first, ideal for banking or hospital priority queues in Islamabad or Lahore.
Common Mistakes & How to Avoid Them
Mistake 1: Misunderstanding Heap Indexing
- Issue: Confusing left/right child indices leads to incorrect swaps.
- Fix: Always use
left = 2*i + 1andright = 2*i + 2for zero-indexed arrays.
Mistake 2: Forgetting to Restore Heap Property After Insert/Extract
- Issue: Heap property violation results in incorrect min/max extraction.
- Fix: Use
_sift_upafter insert and_sift_downafter extract.

Practice Exercises
Exercise 1: Build Your Own Max-Heap
Problem: Create a max-heap and insert values [15, 30, 10, 40, 20]. Extract the maximum element.
Solution:
import heapq
numbers = [15, 30, 10, 40, 20]
max_heap = [-num for num in numbers] # Negate for max-heap
heapq.heapify(max_heap)
print(-heapq.heappop(max_heap)) # Output: 40
Exercise 2: Hospital Bed Priority Queue
Problem: Patients in Lahore hospital have priority levels (1 = high, 5 = low). Implement a priority queue.
Solution:
import heapq
patients = [(3, 'Ahmed'), (1, 'Fatima'), (4, 'Ali'), (2, 'Sara')]
heapq.heapify(patients)
while patients:
priority, name = heapq.heappop(patients)
print(f"Attending {name} with priority {priority}")
Frequently Asked Questions
What is a heap data structure?
A heap is a complete binary tree that maintains a specific order: min-heap (smallest root) or max-heap (largest root). It allows efficient access to the minimum or maximum element.
How do I implement a priority queue in Python?
You can use the heapq module to implement a priority queue. Use heappush to add elements and heappop to retrieve the highest-priority element.
What is the time complexity of heap operations?
- Insertion: O(log n)
- Deletion (extract): O(log n)
- Heapify: O(n)
What is heap sort and how does it work?
Heap sort builds a max-heap (or min-heap) from an array, repeatedly extracts the root, and rearranges the array. It guarantees O(n log n) sorting time.
Where are heaps used in real life?
Heaps are used in priority queues, task scheduling, Dijkstra’s algorithm, and real-time systems like banking queues or hospital management.
Summary & Key Takeaways
- Heaps are complete binary trees with a specific min/max ordering.
- Min-heaps give quick access to the smallest element, max-heaps to the largest element.
- Heaps efficiently implement priority queues in O(log n) time for insertion and extraction.
- Python’s
heapqmodule simplifies heap operations for practical applications. - Heap sort uses a heap to sort elements efficiently with O(n log n) complexity.
Next Steps & Related Tutorials
- Learn Sorting Algorithms to complement heap sort knowledge.
- Explore Binary Trees for better understanding of tree-based structures.
- Check Stacks and Queues to understand basic data structures before advanced heaps.
- Study Graphs & BFS/DFS to see heaps in graph algorithms like Dijkstra’s shortest path.
✅ This tutorial is fully optimized for heap data structure, priority queue tutorial, min heap max heap, with Pakistani-relevant examples and practical Python implementations.
If you want, I can also create all the images placeholders into fully labeled diagrams ready for your web page so the tutorial is visually complete for theiqra.edu.pk.
Do you want me to do that next?
Test Your Python Knowledge!
Finished reading? Take a quick quiz to see how much you've learned from this tutorial.