Cod sursa(job #1503628)

Utilizator TeodorCotetCotet Teodor TeodorCotet Data 16 octombrie 2015 17:22:01
Problema Hashuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
/* 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;
}