Pagini recente » Cod sursa (job #1514007) | Cod sursa (job #1984214) | Cod sursa (job #1240483) | Cod sursa (job #1513984) | Cod sursa (job #1511518)
#include <fstream>
using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
struct Nod
{
int Info;
Nod * Left, * Right;
Nod()
{
Info = 0;
Left = Right = 0;
}
};
Nod * BST = NULL;
int N;
void Insert(Nod *& p, int Val)
{
if( !p)
{
p = new Nod;
p->Info = Val;
return;
}
if(Val <= p->Info)
Insert(p->Left,Val);
else
Insert(p->Right, Val);
}
void Delete2(Nod *&node, Nod *& p)
{
if(p->Right)
{
Delete2(node,p->Right);
return;
}
node->Info = p->Info;
Nod * q = p;
p = p -> Left;
delete q;
}
void Delete(Nod *& p,int Val)
{
if(!p)
return;
if(Val < p->Info)
Delete(p->Left,Val);
else
if(Val > p->Info)
Delete(p->Right, Val);
else
{
if(p -> Left == 0)
{
Nod * q = p;
p = p -> Right;
delete q;
}
else
if(p -> Right == 0)
{
Nod * q = p;
p = p -> Left;
delete q;
}
else
Delete2(p,p->Left);
}
}
Nod * Find(Nod * p, int Val)
{
while(p && p->Info!=Val)
{
if(Val < p->Info)
p = p->Left;
else
p = p->Right;
}
return p;
}
int main()
{
fin>>N;
for (int i = 1;i <= N; ++i)
{
int op,x;
fin>>op>>x;
if (op == 1)
{
Insert(BST,x);
}
if (op == 2)
{
Delete(BST,x);
}
if (op == 3)
{
fout<< (Find(BST,x)!=0) <<"\n";
}
}
return 0;
}