|
| 1 | +<h1 align='center'>Heap - Data - Structure - Example STL</h1> |
| 2 | + |
| 3 | +In C++, the Standard Template Library (STL) provides an efficient way to implement heaps (priority queues) using the `priority_queue` container. This makes it easy to create both max-heaps and min-heaps without having to implement the heap operations manually. Let’s go over the code you provided and explain each part, as well as the specifics of using STL for heaps. |
| 4 | + |
| 5 | +## Source code |
| 6 | +```cpp |
| 7 | +#include <iostream> |
| 8 | +#include <queue> |
| 9 | +#include <vector> |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +int main() { |
| 13 | + // Creating Max and Min Heap using STL. |
| 14 | + |
| 15 | + // ------ Creation of Max Heap ------ |
| 16 | + cout << "------ Creation of Max Heap ------" << endl; |
| 17 | + priority_queue<int> max_pq; // Max-Heap by default |
| 18 | + |
| 19 | + // Adding elements to the Max-Heap |
| 20 | + max_pq.push(4); |
| 21 | + max_pq.push(2); |
| 22 | + max_pq.push(5); |
| 23 | + max_pq.push(3); |
| 24 | + |
| 25 | + // Displaying the top element of the Max-Heap |
| 26 | + cout << "Element at Top : " << max_pq.top() << endl; // Should display the maximum element |
| 27 | + max_pq.pop(); // Removes the top (maximum) element |
| 28 | + cout << "Element at Top : " << max_pq.top() << endl; // Displays the new top element after pop |
| 29 | + |
| 30 | + // Displaying the size of the Max-Heap |
| 31 | + cout << "Size of priority queue : " << max_pq.size() << endl; |
| 32 | + |
| 33 | + // Checking if the Max-Heap is empty |
| 34 | + if (max_pq.empty()) |
| 35 | + cout << "Priority queue is empty" << endl; |
| 36 | + else |
| 37 | + cout << "Priority queue is not empty" << endl; |
| 38 | + |
| 39 | + // ------ Creation of Min Heap ------ |
| 40 | + cout << endl << "------ Creation of Min Heap ------" << endl; |
| 41 | + priority_queue<int, vector<int>, greater<int>> min_pq; // Min-Heap |
| 42 | + |
| 43 | + // Adding elements to the Min-Heap |
| 44 | + min_pq.push(4); |
| 45 | + min_pq.push(2); |
| 46 | + min_pq.push(5); |
| 47 | + min_pq.push(3); |
| 48 | + |
| 49 | + // Displaying the top element of the Min-Heap |
| 50 | + cout << "Element at Top : " << min_pq.top() << endl; // Should display the minimum element |
| 51 | + min_pq.pop(); // Removes the top (minimum) element |
| 52 | + cout << "Element at Top : " << min_pq.top() << endl; // Displays the new top element after pop |
| 53 | + |
| 54 | + // Displaying the size of the Min-Heap |
| 55 | + cout << "Size of priority queue : " << min_pq.size() << endl; |
| 56 | + |
| 57 | + // Checking if the Min-Heap is empty |
| 58 | + if (min_pq.empty()) |
| 59 | + cout << "Priority queue is empty" << endl; |
| 60 | + else |
| 61 | + cout << "Priority queue is not empty" << endl; |
| 62 | + |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | +## Source Code Explanation |
| 69 | + |
| 70 | +1. **Including Libraries** |
| 71 | + ```cpp |
| 72 | + #include <iostream> |
| 73 | + #include <queue> |
| 74 | + #include <vector> |
| 75 | + using namespace std; |
| 76 | + ``` |
| 77 | + |
| 78 | + - `iostream`: Used for input and output. |
| 79 | + - `queue`: Contains the `priority_queue` class, which is used to implement heaps. |
| 80 | + - `vector`: Required when we use `priority_queue` with a custom comparator (for min-heaps in this example). |
| 81 | + |
| 82 | +2. **Creating a Max-Heap** |
| 83 | + |
| 84 | + ```cpp |
| 85 | + priority_queue<int> max_pq; // Max-Heap by default |
| 86 | + ``` |
| 87 | + |
| 88 | + - The default `priority_queue` in C++ is a max-heap, meaning the largest element has the highest priority. |
| 89 | + - We can add elements using `push`, and the element with the highest value will be at the top. |
| 90 | + |
| 91 | +3. **Operations on Max-Heap** |
| 92 | + |
| 93 | + ```cpp |
| 94 | + max_pq.push(4); |
| 95 | + max_pq.push(2); |
| 96 | + max_pq.push(5); |
| 97 | + max_pq.push(3); |
| 98 | + ``` |
| 99 | + |
| 100 | + - The `push` function inserts elements into the heap. |
| 101 | + - Since `priority_queue` is a max-heap by default, after adding these elements, the heap will automatically arrange them so the maximum element is at the top. |
| 102 | + |
| 103 | +4. **Accessing and Removing Elements** |
| 104 | + |
| 105 | + ```cpp |
| 106 | + cout << "Element at Top : " << max_pq.top() << endl; // Displays the maximum element |
| 107 | + max_pq.pop(); // Removes the maximum element (top) |
| 108 | + cout << "Element at Top : " << max_pq.top() << endl; |
| 109 | + ``` |
| 110 | + |
| 111 | + - `top`: Returns the largest element (the root of the max-heap) without removing it. |
| 112 | + - `pop`: Removes the largest element from the heap. After `pop`, the next largest element moves to the top. |
| 113 | + |
| 114 | +5. **Checking Size and Emptiness** |
| 115 | + |
| 116 | + ```cpp |
| 117 | + cout << "Size of priority queue : " << max_pq.size() << endl; |
| 118 | + if (max_pq.empty()) |
| 119 | + cout << "Priority queue is empty" << endl; |
| 120 | + else |
| 121 | + cout << "Priority queue is not empty" << endl; |
| 122 | + ``` |
| 123 | + |
| 124 | + - `size()`: Returns the number of elements in the heap. |
| 125 | + - `empty()`: Checks if the heap is empty. |
| 126 | + |
| 127 | +6. **Creating a Min-Heap** |
| 128 | + |
| 129 | + ```cpp |
| 130 | + priority_queue<int, vector<int>, greater<int>> min_pq; // Min-Heap |
| 131 | + ``` |
| 132 | + |
| 133 | + - Unlike the default max-heap, a min-heap requires a custom comparator. |
| 134 | + - `greater<int>` specifies that the heap should order elements in ascending order, making it a min-heap. |
| 135 | + - `vector<int>` is the underlying container used to store the heap elements. |
| 136 | + |
| 137 | +7. **Operations on Min-Heap** |
| 138 | + |
| 139 | + The operations (`push`, `top`, `pop`, `size`, `empty`) work similarly to those in the max-heap. However, because of the `greater<int>` comparator, the smallest element will be at the top. |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +### STL Implementation of `priority_queue` |
| 144 | + |
| 145 | +The `priority_queue` class in the STL provides an implementation of a binary heap: |
| 146 | + |
| 147 | +1. **Max-Heap (Default)** |
| 148 | + - The default `priority_queue` in C++ is implemented as a max-heap, where the largest element is at the root. |
| 149 | + - The internal representation is typically an array, where each parent node is greater than its child nodes. |
| 150 | + |
| 151 | +2. **Min-Heap** |
| 152 | + - To create a min-heap, we can pass a custom comparator, like `greater<int>`. |
| 153 | + - This changes the heap property to maintain the smallest element at the root, rather than the largest. |
| 154 | + |
| 155 | +### Complexity Analysis |
| 156 | + |
| 157 | +- **Insertion (`push`)**: \(O(\log n)\) |
| 158 | +- **Accessing Top Element (`top`)**: \(O(1)\) |
| 159 | +- **Removing Top Element (`pop`)**: \(O(\log n)\) |
| 160 | + |
| 161 | +### Example Output |
| 162 | + |
| 163 | +For the given input elements, the output would be: |
| 164 | + |
| 165 | +```plaintext |
| 166 | +------ Creation of Max Heap ------ |
| 167 | +Element at Top : 5 |
| 168 | +Element at Top : 4 |
| 169 | +Size of priority queue : 3 |
| 170 | +Priority queue is not empty |
| 171 | +
|
| 172 | +------ Creation of Min Heap ------ |
| 173 | +Element at Top : 2 |
| 174 | +Element at Top : 3 |
| 175 | +Size of priority queue : 3 |
| 176 | +Priority queue is not empty |
| 177 | +``` |
| 178 | + |
| 179 | +### Summary |
| 180 | + |
| 181 | +The `priority_queue` in C++ is a flexible and efficient way to manage heaps. The default max-heap can be used directly for cases where the largest element should be given priority, and a min-heap can be created by specifying a custom comparator. Each of these operations is optimized to provide logarithmic time complexity for insertions and deletions, making `priority_queue` suitable for priority-based task management and similar applications. |
0 commit comments