#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
string s;
int x,m,i;
struct node
{
int data;
struct node * lchild;
struct node * rchild;
};
typedef struct node;
typedef node * tree;
tree root;
void findn(tree bt,int n)
{
if(bt)
{
if(n<bt->data) findn(bt->lchild,n);
else if(n>bt->data) findn(bt->rchild,n);
else cout<<"yes"<<endl;
}
else cout<<"no"<<endl;
}
void insert(tree &bt,int n)
{
if(bt)
{
if(n<bt->data)insert(bt->lchild,n);
else if (n>bt->data)insert(bt->rchild,n);
}
else
{
bt=new node;
bt->data=n;
bt->lchild=bt->rchild=NULL;
}
}

void printh(tree bt)
{
if(bt)
{
if(bt->data!=-99)
cout<<" "<<bt->data;
printh(bt->lchild);
printh(bt->rchild);

}
}
void printz(tree bt)
{
if(bt)
{
printz(bt->lchild);
if(bt->data!=-99)
cout<<" "<<bt->data;
printz(bt->rchild);
}
}
void delet(tree &bt,int z)
{
if(bt->data>z)
delet(bt->lchild,z);
else if(bt->data<z)
delet(bt->rchild,z);
else
{
if(bt->lchild&&bt->rchild)
{
tree tmp=bt->rchild;
while(tmp->lchild)
{
tmp=tmp->lchild;
}
bt->data=tmp->data;
delet(bt->rchild,tmp->data);
}
else
{
tree tmp=bt;
if(bt->rchild!=NULL)
{
bt=bt->rchild;
}
else if(bt->lchild!=NULL)
{
bt=bt->lchild;
}
else
bt->data=-99;
}
}
}
int main()
{
root=NULL;
int n;
cin>>n;
for(int i=1;i<=n;i++)
{

cin>>s;

if(s=="insert")
{
cin>>x;
insert(root,x);
}
if(s=="find")
{
cin>>x;
findn(root,x);
}
if(s=="print")
{
printz(root);
cout<<endl;
printh(root);
cout<<endl;
}
if(s=="delete")
{
int c;
scanf("%d",&c);
delet(root,c);
}
}

//system("pause");
return 0;
}