Pagini recente » Cod sursa (job #3036654) | Cod sursa (job #3167122)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
struct nod{
int val;
nod *stg;
nod *dr;
int h;
};
int h(nod *N) {
if (N == NULL)
return 0;
return N->h;
}
int max(int a, int b) {
return (a > b) ? a : b;
}
nod *newNode(int key) {
nod *node = new nod();
node->val = key;
node->stg = NULL;
node->dr = NULL;
node->h = 1;
return (node);
}
nod *rightRotate(nod *y) {
nod *x = y->stg;
nod *T2 = x->dr;
x->dr = y;
y->stg = T2;
y->h = max(h(y->stg), h(y->dr)) + 1;
x->h = max(h(x->stg), h(x->dr)) + 1;
return x;
}
nod *leftRotate(nod *x) {
nod *y = x->dr;
nod *T2 = y->stg;
y->stg = x;
x->dr = T2;
x->h = max(h(x->stg), h(x->dr)) + 1;
y->h = max(h(y->stg), h(y->dr)) + 1;
return y;
}
int getBalanceFactor(nod *N) {
if (N == NULL)
return 0;
return h(N->stg) - h(N->dr);
}
nod *insertNode(nod *node, int key) {
if (node == NULL)
return (newNode(key));
if (key < node->val)
node->stg = insertNode(node->stg, key);
else if (key > node->val)
node->dr = insertNode(node->dr, key);
else
return node;
node->h = 1 + max(h(node->stg), h(node->dr));
int balanceFactor = getBalanceFactor(node);
if (balanceFactor > 1) {
if (key < node->stg->val) {
return rightRotate(node);
} else if (key > node->stg->val) {
node->stg = leftRotate(node->stg);
return rightRotate(node);
}
}
if (balanceFactor < -1) {
if (key > node->dr->val) {
return leftRotate(node);
} else if (key < node->dr->val) {
node->dr = rightRotate(node->dr);
return leftRotate(node);
}
}
return node;
}
nod *nodeWithMimumValue(nod *node) {
nod *current = node;
while (current->stg != NULL)
current = current->stg;
return current;
}
nod *deleteNode(nod *root, int key) {
if (root == NULL)
return root;
if (key < root->val)
root->stg = deleteNode(root->stg, key);
else if (key > root->val)
root->dr = deleteNode(root->dr, key);
else {
if ((root->stg == NULL) ||
(root->dr == NULL)) {
nod *temp = root->stg ? root->stg : root->dr;
if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp;
free(temp);
} else {
nod *temp = nodeWithMimumValue(root->dr);
root->val = temp->val;
root->dr = deleteNode(root->dr,
temp->val);
}
}
if (root == NULL)
return root;
root->h = 1 + max(h(root->stg),
h(root->dr));
int balanceFactor = getBalanceFactor(root);
if (balanceFactor > 1) {
if (getBalanceFactor(root->stg) >= 0) {
return rightRotate(root);
} else {
root->stg = leftRotate(root->stg);
return rightRotate(root);
}
}
if (balanceFactor < -1) {
if (getBalanceFactor(root->dr) <= 0) {
return leftRotate(root);
} else {
root->dr = rightRotate(root->dr);
return leftRotate(root);
}
}
return root;
}
//void printTree(nod *root, string indent, bool last) {
// if (root != nullptr) {
// cout << indent;
// if (last) {
// cout << "R----";
// indent += " ";
// } else {
// cout << "L----";
// indent += "| ";
// }
// cout << root->val << endl;
// printTree(root->stg, indent, false);
// printTree(root->dr, indent, true);
// }
//}
int v[200000];
int main() {
nod *root = NULL;
int n, c, nr, idx=0;
fin>>n;
for(int i=0;i<n;i++){
fin>>c;
if(c==1){
fin>>nr;
root=insertNode(root, nr);
v[++idx]=nr;
}
else if(c==2){
fin>>nr;
root = deleteNode(root, v[nr]);
}
else{
fout<<nodeWithMimumValue(root)->val<<"\n";
}
// printTree(root, "", true);
// cout<<"|||||||||||||||||||||||||||||\n";
}
}