Answers for "priority queue min heap"

2

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;
}
Posted by: Guest on September-13-2021
16

min heap in c++

priority_queue <int, vector<int>, greater<int>> minHeap;
Posted by: Guest on August-02-2020
5

min heap priority queue c++

#include<queue>
std::priority_queue <int, std::vector<int>, std::greater<int> > minHeap;
Posted by: Guest on May-22-2020
1

priority queue with min heap

This is frequently used in Competitive Programming.
We first multiply all elements with (-1). Then we create a max heap (max heap is the default for priority queue). 
When we access the data and want to print it we simply multiply those elements with (-1) again.
Posted by: Guest on July-31-2021
1

create a min heap in java using priority queue

int arr[]={1,2,1,3,3,5,7};
        PriorityQueue<Integer> a=new PriorityQueue<>();
        for(int i:arr){
            a.add(i);
        }
        while(!a.isEmpty())
            System.out.println(a.poll());
Posted by: Guest on September-08-2020
1

priority queue min heap

#include <bits/stdc++.h>
using namespace std;
 
// User defined class, Point
class Point
{
   int x;
   int y;
public:
   Point(int _x, int _y)
   {
      x = _x;
      y = _y;
   }
   int getX() const { return x; }
   int getY() const { return y; }
};
 
// To compare two points
class myComparator
{
public:
    int operator() (const Point& p1, const Point& p2)
    {
        return p1.getX() > p2.getX();
    }
};
 
// Driver code
int main ()
{
    // Creates a Min heap of points (order by x coordinate)
    priority_queue <Point, vector<Point>, myComparator > pq;
 
    // Insert points into the min heap
    pq.push(Point(10, 2));
    pq.push(Point(2, 1));
    pq.push(Point(1, 5));
 
    // One by one extract items from min heap
    while (pq.empty() == false)
    {
        Point p = pq.top();
        cout << "(" << p.getX() << ", " << p.getY() << ")";
        cout << endl;
        pq.pop();
    }
 
    return 0;
}
Posted by: Guest on March-15-2021

Code answers related to "priority queue min heap"

Browse Popular Code Answers by Language