difference between heap and priority queue code example
Example 1: priority queue min heap
#include <bits/stdc++.h>
using namespace std;
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; }
};
class myComparator
{
public:
int operator() (const Point& p1, const Point& p2)
{
return p1.getX() > p2.getX();
}
};
int main ()
{
priority_queue <Point, vector<Point>, myComparator > pq;
pq.push(Point(10, 2));
pq.push(Point(2, 1));
pq.push(Point(1, 5));
while (pq.empty() == false)
{
Point p = pq.top();
cout << "(" << p.getX() << ", " << p.getY() << ")";
cout << endl;
pq.pop();
}
return 0;
}
Example 2: Difference between Priority Queue and Heap
This website provides a really clear explanation. http:
In short, a priority queue can be implemented using many of the data structures that we've already studied (an array, a linked list, or a binary search tree). However, those data structures do not provide the most efficient operations. To make all of the operations very efficient, we'll use a new data structure called a heap.