|
| 1 | +#include <iostream> |
| 2 | +#include <queue> |
| 3 | +#include <vector> |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +int main() { |
| 7 | + // Creating Max and Min Heap using STL. |
| 8 | + |
| 9 | + // ------ Creation of Max Heap ------ |
| 10 | + cout << "------ Creation of Max Heap ------" << endl; |
| 11 | + priority_queue<int> max_pq; // Max-Heap by default |
| 12 | + |
| 13 | + // Adding elements to the Max-Heap |
| 14 | + max_pq.push(4); |
| 15 | + max_pq.push(2); |
| 16 | + max_pq.push(5); |
| 17 | + max_pq.push(3); |
| 18 | + |
| 19 | + // Displaying the top element of the Max-Heap |
| 20 | + cout << "Element at Top : " << max_pq.top() << endl; // Should display the maximum element |
| 21 | + max_pq.pop(); // Removes the top (maximum) element |
| 22 | + cout << "Element at Top : " << max_pq.top() << endl; // Displays the new top element after pop |
| 23 | + |
| 24 | + // Displaying the size of the Max-Heap |
| 25 | + cout << "Size of priority queue : " << max_pq.size() << endl; |
| 26 | + |
| 27 | + // Checking if the Max-Heap is empty |
| 28 | + if (max_pq.empty()) |
| 29 | + cout << "Priority queue is empty" << endl; |
| 30 | + else |
| 31 | + cout << "Priority queue is not empty" << endl; |
| 32 | + |
| 33 | + // ------ Creation of Min Heap ------ |
| 34 | + cout << endl << "------ Creation of Min Heap ------" << endl; |
| 35 | + priority_queue<int, vector<int>, greater<int>> min_pq; // Min-Heap |
| 36 | + |
| 37 | + // Adding elements to the Min-Heap |
| 38 | + min_pq.push(4); |
| 39 | + min_pq.push(2); |
| 40 | + min_pq.push(5); |
| 41 | + min_pq.push(3); |
| 42 | + |
| 43 | + // Displaying the top element of the Min-Heap |
| 44 | + cout << "Element at Top : " << min_pq.top() << endl; // Should display the minimum element |
| 45 | + min_pq.pop(); // Removes the top (minimum) element |
| 46 | + cout << "Element at Top : " << min_pq.top() << endl; // Displays the new top element after pop |
| 47 | + |
| 48 | + // Displaying the size of the Min-Heap |
| 49 | + cout << "Size of priority queue : " << min_pq.size() << endl; |
| 50 | + |
| 51 | + // Checking if the Min-Heap is empty |
| 52 | + if (min_pq.empty()) |
| 53 | + cout << "Priority queue is empty" << endl; |
| 54 | + else |
| 55 | + cout << "Priority queue is not empty" << endl; |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
0 commit comments