Skip to content

Commit

Permalink
Create priority_queue.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
radicalparty authored Sep 6, 2024
1 parent a7dde65 commit 0d9e197
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions data_structure/queue/priority_queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include <bits/stdc++.h>
using namespace std;

template<typename T>
class pq {
private:
int size, height;
struct Node {
T value;
Node *l, *r, *parent;
Node(T val) : value(val), l(nullptr), r(nullptr), parent(nullptr) {} // Node constructor
};
Node* root;

public:
pq() : root(nullptr), size(0), height(0) {}

void insert(T value) {
size++;
if ((size & (size - 1)) == 0) height++;
if (size == 1) {
root = new Node(value);
return;
}

Node* now = root;
for (int i = height - 2; i >= 0; i--) {
if (((1 << i) & size) == 0) {
if (!now->l) now->l = new Node(0);
now->l->parent = now;
now = now->l;
} else {
if (!now->r) now->r = new Node(0);
now->r->parent = now;
now = now->r;
}
}
now->value = value;

while (now->parent && now->value < now->parent->value) {
swap(now->value, now->parent->value);
now = now->parent;
}
}

void pop() {
if (size == 0) return;
Node* now = root;

for (int i = height - 2; i >= 0; i--) {
if (((1 << i) & size) == 0) {
now = now->l;
} else {
now = now->r;
}
}

if (size > 1) {
swap(root->value, now->value);
}

if (now->parent) {
if (now == now->parent->l) now->parent->l = nullptr;
else now->parent->r = nullptr;
}
delete now;

if ((size & (size - 1)) == 0) height--;
size--;

now = root;
while (now && (now->l || now->r)) {
Node* smallest = now;
if (now->l && now->l->value < smallest->value) smallest = now->l;
if (now->r && now->r->value < smallest->value) smallest = now->r;
if (smallest == now) break;
swap(now->value, smallest->value);
now = smallest;
}
}

T top() {
return root->value;
}

bool empty() const{
return size == 0;
}
};

int main() {
cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
pq<int> test;
int n; cin >> n;
while (n--){
int x; cin >> x;
if (x) test.insert(x);
else{
if (test.empty()) cout << 0 << "\n";
else {
cout << test.top() << "\n"; test.pop();
}
}
}
return 0;
}

0 comments on commit 0d9e197

Please sign in to comment.