From 6240353c8c4044cdfcd46190055b7df52d258e89 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Mon, 20 Nov 2023 16:58:55 -0500 Subject: [PATCH] adding min heap notes --- lectures/22_priority_queues/README.md | 63 +++++++++++++++++++++++- lectures/22_priority_queues/max_heap.cpp | 20 ++++++++ lectures/22_priority_queues/min_heap.cpp | 20 ++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 lectures/22_priority_queues/max_heap.cpp create mode 100644 lectures/22_priority_queues/min_heap.cpp diff --git a/lectures/22_priority_queues/README.md b/lectures/22_priority_queues/README.md index 64ee836..5e6d1c7 100644 --- a/lectures/22_priority_queues/README.md +++ b/lectures/22_priority_queues/README.md @@ -134,11 +134,72 @@ Lambda is new to the C++ language (part of C++11). But lambda is a core piece of - Draw several other trees with these values which are not binary heaps. -## 22.7 Implementing a Heap with a Vector (instead of Nodes & Pointers) +## 22.7 STL priority_queue - The standard library (STL) priority_queue is implemented as a binary heap. - The STL priority_queue is a max heap. +- You need to include <queue> in order to use the STL priority_queue. Below is a simple [example](max_heap.cpp): + +#include +#include + +int main() { + std::priority_queue maxHeap; + + maxHeap.push(3); + maxHeap.push(4); + maxHeap.push(3); + maxHeap.push(1); + maxHeap.push(5); + + while (!maxHeap.empty()) { + std::cout << maxHeap.top() << " "; + maxHeap.pop(); + } + std::cout << std::endl; + + return 0; +} + +- The above program will print: + +```console +5 4 3 3 1 +``` + - When using std::priority_queue to store class objects, oftentimes, you need to define a class and overload its function call operator; or use a lambda expression. +- You can use std::priority_queue as a min heap via using std::greater, as can be seen in this [example](min_heap.cpp): + +```cpp +#include +#include + +int main() { + std::priority_queue, std::greater> minHeap; + + minHeap.push(3); + minHeap.push(4); + minHeap.push(3); + minHeap.push(1); + minHeap.push(5); + + while (!minHeap.empty()) { + std::cout << minHeap.top() << " "; + minHeap.pop(); + } + std::cout << std::endl; + + return 0; +} +``` + +- This above program will print: + +```console +1 3 3 4 5 +``` + +- If you just define the priority queue with std::priority_queue, you will get a max heap. ## 22.8 Leetcode Exercises diff --git a/lectures/22_priority_queues/max_heap.cpp b/lectures/22_priority_queues/max_heap.cpp new file mode 100644 index 0000000..15352af --- /dev/null +++ b/lectures/22_priority_queues/max_heap.cpp @@ -0,0 +1,20 @@ +#include +#include + +int main() { + std::priority_queue maxHeap; + + maxHeap.push(3); + maxHeap.push(4); + maxHeap.push(3); + maxHeap.push(1); + maxHeap.push(5); + + while (!maxHeap.empty()) { + std::cout << maxHeap.top() << " "; + maxHeap.pop(); + } + std::cout << std::endl; + + return 0; +} diff --git a/lectures/22_priority_queues/min_heap.cpp b/lectures/22_priority_queues/min_heap.cpp new file mode 100644 index 0000000..bc021eb --- /dev/null +++ b/lectures/22_priority_queues/min_heap.cpp @@ -0,0 +1,20 @@ +#include +#include + +int main() { + std::priority_queue, std::greater> minHeap; + + minHeap.push(3); + minHeap.push(4); + minHeap.push(3); + minHeap.push(1); + minHeap.push(5); + + while (!minHeap.empty()) { + std::cout << minHeap.top() << " "; + minHeap.pop(); + } + std::cout << std::endl; + + return 0; +}