// 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.0136183877309595x):
#include <stdio.h>

#include <stdlib.h>

typedef struct Node{

    int data;

    struct Node *parent, *left, *right;

}Node;

Node *root;

void insert(int x){

    Node *t = (Node *)malloc(sizeof(Node));

    t->data = x;

    t->left = t->right = t->parent = NULL;



    if(root == NULL){

        root = t;

    }else{

        Node *child = root;

        Node *parent = NULL;

        while(child != NULL){

            parent = child;

            if(x < child->data){

                child = child->left;

            }

            else{

                child = child->right;

            }

        }

        t->parent = parent;

        if(x < parent->data){

            parent->left = t;

        }else{

            parent->right = t;

        }

    }

}

void Preorder(Node *t){

    if(t == NULL) return ;

    printf(" %d", t->data);

    Preorder(t->left);

    Preorder(t->right);

}

void Inorder(Node *t){

    if(t == NULL) return ;

    Inorder(t->left);

    printf(" %d", t->data);

    Inorder(t->right);

}

void find(int b){

    Node *temp = root;

    while(temp != NULL){

        if(temp->data == b){

            printf("yes\n");

            return ;

        }

        if(b < temp->data){

            temp = temp->left;

        }

        else{

            temp = temp->right;

        }

    }

    printf("no\n");

}

int main(){

    root = NULL;

    int n;

    scanf("%d", &n);

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

        char a[10]; int b;

        scanf("%s", a);

        if(a[0] == 'i'){

            scanf("%d", &b);

            insert(b);

        }

        else if(a[0] == 'p'){

            Inorder(root);

            printf("\n");

            Preorder(root);

            printf("\n");

        }

        else if(a[0] == 'f'){

            int c;

            scanf("%d", &c);

            find(c);

        }

    }

    return 0;

}