// 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.051973341789658x):
#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<ctime>

#define MAXN 500000

struct node

{

	int key;

	node *ch[2];

};

node tree[MAXN+5];

node *Root,*NIL,*ncnt;

//bool first=true;

void Init()

{

	NIL=&tree[0];

	ncnt=&tree[1];

	Root=NIL;

}

inline node *NewNode(int val)

{

	node *p=++ncnt;

	p->key=val;

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

	return p;

}

void Insert(node *&rt,int val)

{

	if(rt==NIL)

	{

		rt=NewNode(val);

		return;

	}

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

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

}

void PreOrder(node *&rt)

{

	if(rt==NIL)

		return;

//	if(first==true)

//		printf("%d",rt->key),first=false;

//	else

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

	PreOrder(rt->ch[0]);

	PreOrder(rt->ch[1]);

}

void InOrder(node *&rt)

{

	if(rt==NIL)

		return;

	InOrder(rt->ch[0]);

//	if(first==true)

//		printf("%d",rt->key),first=false;

//	else

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

	InOrder(rt->ch[1]);

}

node *Find(node *rt,int val)

{

	if(rt==NIL)

		return NIL;

	if(rt->key==val)

		return rt;

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

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

}

int main()

{

	Init();

	int n,x;

	char op[100];

	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]=='p')

		{

			InOrder(Root);

			printf("\n");

			PreOrder(Root);

			printf("\n");

		}

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

		{

			scanf("%d",&x);

			if(Find(Root,x)==NIL)

				printf("no\n");

			else

				printf("yes\n");

		}

	}

	return 0;

}