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

#define MAXN 500000

struct node{

	int key;

	node *ch[2],*fa;

};

node tree[MAXN+5];

node *Root,*NIL,*ncnt;

void Init(){

	NIL=&tree[0];

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

	ncnt=&tree[1];

	Root=NIL;

}

inline node * NewNode(int val){

	node *p=++ncnt;

	p->key=val;

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

	return p;

}

void Insert(node *&rt,node *fa,int val){

	if(rt==NIL){

		rt=NewNode(val);

		rt->fa=fa;

		return ;

	}

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

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

}

void InOrder(node *rt){

	if(rt==NIL)return ;

	InOrder(rt->ch[0]);

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

	InOrder(rt->ch[1]);

}

void PreOrder(node *rt){

	if(rt==NIL)return ;

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

	PreOrder(rt->ch[0]);

	PreOrder(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(){

	int n,x;

	char op[100];

	Init();

	scanf("%d",&n);

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

	{

		scanf("%s",op);

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

		{

			InOrder(Root);puts("");

			PreOrder(Root);puts("");

		}

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

		{

			scanf("%d",&x);

			Insert(Root,NIL,x);

		}

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

		{

			scanf("%d",&x);

			node *p=Find(Root,x);

			if(p==NIL)puts("no");

			else puts("yes");

		}

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

		{

			

		}

	}

	return 0;

}