Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

min heap and max heap using priority queue

#include <bits/stdc++.h>
#define cl cout << endl;
using namespace std;
int main()
{
    priority_queue<int, vector<int>,greater<int>> pqm; //min heap
    priority_queue<int, vector<int>> pq;  //max heap
    vector<int> v = {100, 2, 89, 36, 42, 91, 555};

    for (int i = 0; i < v.size(); i++)
    {

        pq.push(v[i]);
        pqm.push(v[i]);
        

        // pq.push(temp);
    }

        cout<<"max heap is: ";
        cl

        while (!pq.empty())
        {
            int temp = pq.top();
            pq.pop();
            cout << temp << endl;
        }

        cout<<"min heap is: ";
        cl

        while (!pqm.empty())
        {
            int temp = pqm.top();
            pqm.pop();
            cout << temp << endl;
        }

    return 0;
}
 
PREVIOUS NEXT
Tagged: #min #heap #max #heap #priority #queue
ADD COMMENT
Topic
Name
9+3 =