Pagini recente » Cod sursa (job #3261313) | Cod sursa (job #475236) | Cod sursa (job #718506) | Cod sursa (job #212689) | Cod sursa (job #1505475)
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;
ifstream fin ("hashuri.in");
ofstream fout ("hashuri.out");
int N;
struct Nod{
int key;
int priority;
Nod* st;
Nod* dr;
Nod(int key ) {
this->st = NULL;
this->dr = NULL;
this->key = key;
this->priority = 0;
}
};
Nod *root;
int interogare(Nod* curent, int key) {
if(curent == NULL) return 0;
if(curent->key == key ) return 1;
if(curent->key > key) return interogare(curent->st, key);
return interogare(curent->dr, key);
}
//priority(father) > prioriy(son)
void adauga(Nod* &curent,const int key) {
if(curent == NULL) {
curent = new Nod(key);
return ;
}
if(key > curent->key) {
adauga(curent->dr, key);
} else {
adauga(curent->st, key);
}
}
void sterge(Nod* father, Nod* curent, int key, const bool direction) {
if(curent == NULL) return ;
if(curent->key == key) {
//stergerea
if(curent->st == NULL) {
if(direction == 1)
father->dr = curent->dr;
else
father->st = curent->dr;
delete curent;
} else {
if(curent->dr == NULL) {
if(direction == 1)
father->dr = curent->st;
else
father->st = curent->st;
delete curent;
} else {
//dr != null, st != null
Nod* father_aux = curent;
Nod* aux = curent->st;
while(aux->dr != NULL) {
father_aux = aux;
aux = aux->dr;
}
curent->key = aux->key;
if(curent->st->dr == NULL)
sterge(father_aux, aux, aux->key, 0);
else
sterge(father_aux, aux, aux->key, 1);
}
}
return;
}
if(curent->key > key)
sterge(curent, curent->st, key, 0);
else
sterge(curent, curent->dr, key, 1);
}
int main() {
Nod* root = NULL;
srand(time(NULL));
int type; int x;
fin >> N;
while(N--) {
fin >> type >> x;
switch(type) {
case 1: adauga(root, x); break;
case 2: sterge(root, root, x, 0); break;
case 3: fout << interogare( root, x) << '\n'; break;
}
}
return 0;
}