Lesson 2
Heaps and Priority Queues in C++
Lesson Overview

Welcome to our exploration of the intriguing worlds of heaps and priority queues. These are powerful data structures used extensively across a range of applications, from job scheduling systems to modeling the stock market. Heaps can efficiently solve problems involving intervals, the nth largest elements, and even sorting. C++'s Standard Template Library (STL) provides a priority_queue class, offering functionality to interact with heaps efficiently.

Quick Overview & Motivation

Heaps are a category of binary trees where every parent node has a specific relationship with its children:

  • In a Min-Heap, every parent node has a value less than or equal to its children.
  • In a Max-Heap, every parent node has a value greater than or equal to its children.

This property allows us to repeatedly access the smallest or largest element, respectively, enabling us to solve numerous problems effortlessly. For example, if you want to find the n-th largest number in a list, using sorting can be costly. By leveraging heaps with C++, we can do this efficiently using the priority_queue.

A priority_queue in C++ is typically implemented as a Max-Heap by default, meaning the highest priority element (the largest element) is at the top. We can customize it to behave as a Min-Heap by using a comparator. Here’s an example:

C++
1std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap; // Min-Heap

Here is how you can do it in C++:

C++
1#include <iostream> 2#include <queue> 3#include <vector> 4 5std::vector<int> findKthLargestElements(const std::vector<int>& nums, int k) { 6 // Min-heap 7 std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap; 8 9 for (int num : nums) { 10 minHeap.push(num); 11 if (minHeap.size() > k) { 12 minHeap.pop(); 13 } 14 } 15 16 std::vector<int> result; 17 while (!minHeap.empty()) { 18 result.push_back(minHeap.top()); 19 minHeap.pop(); 20 } 21 std::reverse(result.begin(), result.end()); 22 return result; 23} 24 25// Test 26int main() { 27 std::vector<int> nums = {3, 2, 1, 5, 6, 4}; 28 int k = 2; 29 std::vector<int> result = findKthLargestElements(nums, k); 30 31 for (int num : result) { 32 std::cout << num << " "; 33 } 34 // Output: 6 5 35 return 0; 36}

Priority queues are an abstraction over heaps that store elements according to their priorities. They are used when objects need to be processed based on priority. For instance, scheduling CPU tasks based on priority is a real-life scenario for priority queues.

What's Next: Practice!

Let's plunge into the exercises, trusting that the best way to learn is by doing. As we solve various problems, you'll build a solid understanding of how these powerful tools can be employed to simplify complex tasks. This will not only prepare you for your interviews but also cultivate a mindset for problem-solving. Welcome aboard!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.