Pagini recente » Cod sursa (job #2132871) | Cod sursa (job #64714) | Cod sursa (job #2297476) | Cod sursa (job #390044) | Cod sursa (job #871237)
Cod sursa(job #871237)
#include<iostream>
#include<fstream>
using namespace std;
struct nod {
int info;
nod *st,*dr;
};
typedef nod* PNOD;
PNOD rad;
void inserare(PNOD &p, int x)
{
if(p==NULL) {
p=new nod;
p->st=NULL;
p->dr=NULL;
p->info=x;
}
else {
if(p->info>x)
inserare(p->st,x);
else if(p->info<x)
inserare(p->dr,x);
}
}
void cautare(PNOD p, PNOD &adr, int x)
{
if(p) {
if(p->info==x)
adr=p;
else if(p->info>x)
cautare(p->st,adr,x);
else cautare(p->dr,adr,x);
}
}
void c3(PNOD &p, PNOD &t)
{
if(p->dr)
c3(p->dr,t);
else {
PNOD aux;
t->info=p->info;
aux=p->st;
delete p;
p=aux;
}
}
void stergere(PNOD &p, int x)
{
if(p==NULL)
return ;
if(p->info==x) {
if(p->st==NULL && p->dr==NULL)
p=NULL;
else if(p->st==NULL) {
PNOD aux;
aux=p->dr;
delete p;
p=aux;
}
else if(p->dr==NULL) {
PNOD aux;
aux=p->st;
delete p;
p=aux;
}
else c3(p->st,p);
}
else if(p->info>x)
stergere(p->st,x);
else stergere(p->dr,x);
}
int main ()
{
int n,i,opt,x;
PNOD adr;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
f>>n;
for(i=1;i<=n;i++) {
f>>opt>>x;
if(opt==1)
inserare(rad,x);
else if(opt==2)
stergere(rad,x);
else {
adr=NULL;
cautare(rad,adr,x);
if(adr)
g<<"1";
else g<<"0";
g<<'\n';
}
}
f.close();
g.close();
}