Pagini recente » Cod sursa (job #1434324) | Cod sursa (job #1321261) | Cod sursa (job #2189959) | Cod sursa (job #516018) | Cod sursa (job #1503628)
/* Tratare coliziuni: h(key, i) = (h'(key) + i) cu i = 0, M */
#include <fstream>
using namespace std;
int N;
struct Nod{
int key;
Nod* st;
Nod* dr;
Nod(int key) {
this->st = NULL;
this->dr = NULL;
this->key = key;
}
};
void adauga(Nod** cpcurent,const int key) {
Nod* curent = *cpcurent;
if(*cpcurent == NULL) {
(*cpcurent) = new Nod(key);
return ;
}
if(key > curent->key) {
if(curent->dr == NULL) {
Nod* node = new Nod(key);
curent->dr = node;
} else adauga(&curent->dr, key);
} else {
if(curent->st == NULL) {
Nod* node = new Nod(key);
curent->st = node;
} else adauga(&curent->st, key);
}
}
void sterge(Nod* curent, int key) {
if(curent == NULL) return ;
if(curent->key == key) {
curent->key = -1;
return;
}
if(curent->key > key)
sterge(curent->st, key);
else
sterge(curent->dr, key);
}
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);
interogare(curent->dr, key);
}
int main() {
ifstream fin ("hashuri.in");
ofstream fout ("hashuri.out");
Nod* root = NULL;
int type; int x;
fin >> N;
while(N--) {
fin >> type >> x;
switch(type) {
case 1: adauga(&root, x); break;
case 2: sterge(root, x); break;
case 3: fout << interogare(root, x) << '\n'; break;
}
}
return 0;
}