Priority Queues
Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in nonincreasing order (hence we can see that each element of the queue has a priority {fixed order}).
Functions used with Priority Queues:
- empty(): Returns whether the queue is empty.
- size(): Returns the size of the queue.
- top(): Returns a reference to the topmost element of the queue.
- pop(): Deletes the first element of the queue.
Sets
Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.
Functions used with Sets:
- size(): Returns the number of elements in the set.
- insert(const x): Adds a new element ‘x’ to the set.
Priority queue of sets can be quite useful for designing complex data structures.
Syntax :
priority_queue<set<data_type>> priorityQueue
This stores set as an element in the max-heap priority queue
Below is the implementation of the max-heap priority queue of set:
C++
#include <bits/stdc++.h>
using namespace std;
void print(priority_queue<set< int > > priorityQueue)
{
while (!priorityQueue.empty()) {
set< int > st = priorityQueue.top();
cout << "[ " ;
for ( auto element : st)
cout << element << ' ' ;
cout << ']' ;
cout << '\n' ;
priorityQueue.pop();
}
}
int main()
{
priority_queue<set< int > > priorityQueue;
set< int > set1;
set1.insert(10);
set1.insert(1);
set1.insert(2);
set1.insert(5);
priorityQueue.push(set1);
set< int > set2;
set2.insert(2);
set2.insert(7);
set2.insert(12);
set2.insert(1);
priorityQueue.push(set2);
set< int > set3;
set3.insert(4);
set3.insert(7);
set3.insert(12);
set3.insert(13);
priorityQueue.push(set3);
print(priorityQueue);
return 0;
}
|
Output[ 4 7 12 13 ]
[ 1 2 7 12 ]
[ 1 2 5 10 ]
By default priority queue is a max-heap, therefore internally for two sets inside the priority queue, the set having a greater first element is the topmost element. If the first element is equal then the second value of sets is compared and so on.
Syntax:
priority_queue<set<data_type>, vector<set<data_type>>, greater<set<data_type>>> priorityQueue
This stores set as an element in the min-heap priority queue
Below is the implementation of the min-heap priority queue of set:
C++
#include <bits/stdc++.h>
using namespace std;
void print(priority_queue<set< int >,
vector<set< int > >,
greater<set< int > > >
priorityQueue)
{
while (!priorityQueue.empty()) {
set< int > st = priorityQueue.top();
cout << "[ " ;
for ( auto element : st)
cout << element << ' ' ;
cout << ']' ;
cout << '\n' ;
priorityQueue.pop();
}
}
int main()
{
priority_queue<set< int >,
vector<set< int > >,
greater<set< int > > >
priorityQueue;
set< int > set1;
set1.insert(10);
set1.insert(1);
set1.insert(2);
set1.insert(5);
priorityQueue.push(set1);
set< int > set2;
set2.insert(2);
set2.insert(7);
set2.insert(12);
set2.insert(1);
priorityQueue.push(set2);
set< int > set3;
set3.insert(4);
set3.insert(7);
set3.insert(12);
set3.insert(13);
priorityQueue.push(set3);
print(priorityQueue);
return 0;
}
|
Output[ 1 2 5 10 ]
[ 1 2 7 12 ]
[ 4 7 12 13 ]
Internally for two sets inside the min-heap priority queue, the set having the smaller first element is the topmost element. If the first element is equal then the second value of sets is compared and so on.