// Slower C++ solution:
#include<stdio.h>

#include<iostream>

#include<algorithm>

#include<vector>

#include<cmath>

#include<cstring>

#include<sstream>

#include<string>

using namespace std;

char a[10];int n;

struct node{

   long long zhi;

   node *left;

   node *right;

   node *parent;

   node():left(NULL),right(NULL),parent(NULL){};

};

node *root;

node *f(node *s,int k,node *p)

{

   if(s==NULL)

    {

        s=new node();

        s->zhi=k;

        s->parent=p;

        if(p==NULL) root=s;

    }

    else if(k<s->zhi)

    {

        s->left=f(s->left,k,s);

    }

    else

    {

        s->right=f(s->right,k,s);

    }

    return s;

}

bool flag;

node *myfind(node *p,long long k)

{

 /*   if(p->zhi==k)

    {

        flag=true;

    }

    if(p->left!=NULL&&k<p->zhi) myfind(p->left,k);

    if(p->right!=NULL&&k>p->zhi) myfind(p->right,k);

    return flag;*/

    while(p!=NULL&&p->zhi!=k)

    {

        if(k<p->zhi) p=p->left;

        else p=p->right;

    }

    return p;

}

node *minx(node *p)

{

    while(p->left!=NULL)

    {

        p=p->left;

    }

    return p;

}

node *xia(node *p)

{

    if(p->right!=NULL) return minx(p->right);

    node *y=p->parent;

    while(y!=NULL&&y->left!=p)

    {

        p=y;

        y=y->parent;

    }

    return y;

}

void mydelete(node *p)

{

    node *y,*x;

    if(p->left==NULL&&p->right==NULL)

    {

        y=p;

    }

    else y=xia(p);

    if(y->left!=NULL)

    {

        x=y->left;

    }else {

        x=y->right;

    }

    if(x!=NULL){

        x->parent=y->parent;

    }

    if(y->parent==NULL){

        root=x;

    }

    else {

        if(y==y->parent->left)

        {

            y->parent->left=x;

        }else{

            y->parent->right=x;

        }

    }

    if(y!=p){

        p->zhi=y->zhi;

    }

}

void f2(node *p)

{

    if(p==NULL) return;

    printf(" %d",p->zhi);

    f2(p->left);

    f2(p->right);

}

void f1(node *p)

{

    if(p==NULL) return ;

    f1(p->left);

    printf(" %d",p->zhi);

    f1(p->right);

}

int main()

{

    int h;

    scanf("%d",&h);

    for(int i=0;i<h;i++)

    {

        scanf("%s",a);

        if(!strcmp(a,"insert"))

        {

            scanf("%d",&n);

            f(root,n,NULL);

        }

        else if(!strcmp(a,"print"))

        {

            f1(root);

            printf("\n");

            f2(root);

            printf("\n");

        }

        else if(!strcmp(a,"find"))

        {

            scanf("%d",&n);

            flag=false;

            node *o=myfind(root,n);

            if(o!=NULL)

            {

                printf("yes\n");

            }

            else printf("no\n");

        }

        else if(!strcmp(a,"delete"))

        {

            scanf("%d",&n);

            mydelete(myfind(root,n));

        }

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.038416955993097x):
#include<cstdio>

#define N 500005

struct mzls

{

	int key;

	mzls *ch[2];

};

mzls tree[N];

bool f1=1;

mzls *root,*NIL,*ncnt;

inline void Init()

{

	NIL=&tree[0];

	NIL->ch[0]=NIL->ch[1]=NIL;

	ncnt=&tree[1];

	root=NIL;

}

inline mzls *newnode(int ml)

{

	mzls *p=++ncnt;

	p->key=ml;

	p->ch[0]=p->ch[1]=NIL;

	return p;

}

inline void Insert(mzls *&rt,int ml)

{

	if(rt==NIL)

	{

		rt=newnode(ml);

		return;

	}

	int d=(ml>=rt->key);

	Insert(rt->ch[d],ml);

}

inline void InOrder(mzls *rt)

{

	if(rt==NIL)

		return;

	InOrder(rt->ch[0]);

	printf(" %d",rt->key);

	InOrder(rt->ch[1]);

}

inline void Preorder(mzls *rt)

{

	if(rt==NIL)

		return;

	printf(" %d",rt->key);

	Preorder(rt->ch[0]);

	Preorder(rt->ch[1]);

}

inline mzls *Find(mzls *rt, int ml) 

{

	if(rt==NIL) return NIL;

	if(rt->key==ml) return rt;

	int d=(ml>=rt->key);

	return Find(rt->ch[d],ml);

}

int main()

{

	int n,x;

	char op[100];

	Init();

	scanf("%d",&n);

	for(int i=0;i<n;i++)

	{

		scanf("%s",op);

		if(op[0]=='i')

		{

			scanf("%d",&x);

			Insert(root,x);

		}

		else if(op[0]=='f') 

		{

			scanf("%d", &x);

			mzls *p=Find(root, x);

			if(p==NIL)

				puts("no");

			else

				puts("yes");

		}

		else

		{

			InOrder(root);

			puts("");

			Preorder(root);

			puts("");

		}

	}

	return 0;

}