代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1e6+10;
int l[N],r[N];
int d[N],idx;
char a[7];
int n;
int h;
void intree(int x,int father)
{
if(x>d[father])
{
if(r[father]==-1)
{
r[father]=++idx;
d[idx]=x;
}else
{
intree(x,r[father]);
}
}else if(x<d[father])
{
if(l[father]==-1)
{
l[father]=++idx;
d[idx]=x;
}else
{
intree(x,l[father]);
}
}else
return;
}
bool get(int x)
{
int p=h;
while(1)
{
if(x==d[p])return true;
else if(x>d[p])
{
if(r[p]==-1)
{
return false;
}else
{
p=r[p];
}
}else
{
if(l[p]==-1)
{
return false;
}else
{
p=l[p];
}
}
}
return false;
}
int get_lmin(int p,int father)
{
if(r[p]==-1)
{
if(l[father]==p)
{
l[father]=l[p];
}else
{
r[father]=l[p];
}
return p;
}else
{
return get_lmin(r[p], p);
}
}
int get_rmin(int p,int father)
{
if(l[p]==-1)
{
if(l[father]==p)
{
l[father]=r[p];
}else
{
r[father]=r[p];
}
return p;
}else
{
return get_rmin(l[p],p);
}
}
void dele(int x,int p,int father)
{
if(d[p]==x)
{
if(l[p]!=-1)
{
d[p]=d[get_lmin(l[p],p)];
}else if(r[p]!=-1)
{
d[p]=d[get_rmin(r[p],p)];
}else if(r[p]==-1&&l[p]==-1)
{
if(d[l[father]]==x)
{
l[father]=-1;
}else
{
r[father]=-1;
}
}
}else if(x>d[p])
{
dele(x,r[p],p);
}else
{
dele(x,l[p],p);
}
}
void output()
{
queue<int>que;
que.push(1);
while(que.size())
{
int t=que.front();
cout<<d[t]<<' ';
que.pop();
if(l[t]!=-1)que.push(l[t]);
if(r[t]!=-1)que.push(r[t]);
}
}
int main()
{
int b;
h=0;
d[h]=1e9;
memset(d,0x3f,sizeof d);
memset(l,-1,sizeof l);
memset(r,-1,sizeof r);
scanf("%d",&n);
while(n--)
{
scanf("%s%d",a+1,&b);
if(a[1]=='i')
{
intree(b,h);
}else if(a[1]=='f')
{
if(get(b))
{
puts("yes");
}else
puts("no");
}else
{
// output();
// cout<<endl;
dele(b,l[h],h);
//output();
}
}
return 0;
}