#include <iostream>
#include <vector>
using namespace std;
template<typename T>
class myPriorityQueue {
private:
vector<T> container;
public:
void push(const T& data) {
container.push_back(data);
int childIdx = static_cast<int>(container.size()) - 1;
while (childIdx > 0) {
int parentIdx = (childIdx - 1) / 2;
if (container[parentIdx] < container[childIdx]) {
T temp = container[parentIdx];
container[parentIdx] = container[childIdx];
container[childIdx] = temp;
childIdx = parentIdx;
}
else {
break;
}
}
}
T& top() {
return container[0];
}
void pop() {
container[0] = container[container.size() - 1];
container.pop_back();
int parentIdx = 0;
while (parentIdx < container.size()) {
int left = parentIdx * 2 + 1;
int right = parentIdx * 2 + 2;
if (left >= container.size()) {
break;
}
else if (right >= container.size()) {
if (container[left] > container[parentIdx]) {
T temp = container[left];
container[left] = container[parentIdx];
container[parentIdx] = temp;
}
break;
}
else {
if (container[left] > container[right]) {
if(container[left] > container[parentIdx]) {
::swap(container[left], container[parentIdx]);
parentIdx = left;
}
else
break;
}
else {
if(container[right] > container[parentIdx]) {
::swap(container[right], container[parentIdx]);
parentIdx = right;
}
else
break;
}
}
}
}
bool empty() {
return container.empty();
}
};
int main() {
myPriorityQueue<int> pq;
pq.push(5);
pq.push(4);
pq.push(1);
pq.push(2);
pq.push(3);
while (!pq.empty()) {
cout << pq.top() << endl;
pq.pop();
}
pq.push(100);
pq.push(300);
pq.push(400);
pq.push(200);
pq.push(500);
while (!pq.empty()) {
cout << pq.top() << endl;
pq.pop();
}
}