#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<vector>
#define MAX 2000000002
#define NIL NULL
using namespace std;
struct node{
    int data=0;
    node *parent=NIL,*left=NIL,*right=NIL;
};

vector<int> PreOrder,InOrder;
node *root=NIL;

void getPre(node *root){
    if(root==NIL) return;
    PreOrder.push_back(root->data);
    getPre(root->left);
    getPre(root->right);
}
void getIn(node *root){
    if(root==NIL) return;
    getIn(root->left);
    InOrder.push_back(root->data);
    getIn(root->right);
}
node *findNode(int num){
    node *temp=root;
    while(temp && temp->data!=num){
        if(temp->data>num)
            temp=temp->left;
        else temp=temp->right;
    }
    return temp;
}
/* void delNode(int num){
    node *aim=findNode(num);
    node *father=root;
    int flag=-1; //left child
    if(aim==NIL) return;
    while(father->left || father->right){
        if(father->left && father->left->data==num){
            flag=0;
            break;
        }else if(father->right && father->right->data==num){
            flag=1;
            break;
        }else if(father->data>num)
            father=father->left;
        else father=father->right;
    }
    if(aim->left==NIL && aim->right==NIL){
        if(flag==0)
            father->left=NIL;
        else father->right=NIL;
        aim->parent=NIL;
    }else if(aim->left && aim->right){
        //has 2 child node
        node *tleft=aim->left;
        while(tleft->left){
            tleft=tleft->left;
        }
        tleft->parent->left=tleft->right;
        tleft->right=aim->right;
        tleft->left=aim->left;
        if(flag==0){
            father->left=tleft;
        }else if(flag==1){
            father->right=tleft;
        }
        tleft->parent=father;
    }else{
        //only one node
        node *tleft=aim->left;
        if(tleft==NIL){
            aim->right->parent=father;
            if(flag==0)
                father->left=aim->right;
            else
                father->right=aim->right;
        }else{
            aim->left->parent=father;
            if(flag==0)
                father->left=aim->right;
            else
                father->right=aim->right;
        }
    }
} */

void delNode(int num){
    node *aim=findNode(num);
    node *father=root;
    int flag=-1; //left child
    if(aim==NIL) return;
    while(father->left || father->right){
        if(father->left && father->left->data==num){
            flag=0;
            break;
        }else if(father->right && father->right->data==num){
            flag=1;
            break;
        }else if(father->data>num)
            father=father->left;
        else father=father->right;
    }
    if(aim->left==NIL && aim->right==NIL){
        if(flag==0)
            father->left=NIL;
        else father->right=NIL;
        aim->parent=NIL;
    }else if(aim->left && aim->right){
        //has 2 child node
        node *tleft=aim->right;
        while(tleft->left){
            tleft=tleft->left;
        }
        if(tleft!=aim->right){
            tleft->parent->left=tleft->right;
            aim->data=tleft->data;
        }else{
            aim->data=aim->right->data;
            aim->right=aim->right->right;
        }

    }else{
        //only one node
        node *tleft=aim->left;
        if(tleft==NIL){
            aim->data=aim->right->data;
            aim->left=aim->right->left;
            aim->right=aim->right->right;
        }else{
            aim->data=aim->left->data;
            aim->right=aim->left->right;
            aim->left=aim->left->left;
        }
    }
}

//void insertToTree(node *root,node *z){
void insertToTree(node *z){
    node *x,*y;
    y=NIL;
    x=root;
    while(x!=NIL){
        y=x;
        if(z->data<x->data){
            x=x->left;
        }else{
            x=x->right;
        }
    }
    z->parent=y;
    if(y==NIL){
        root=z;
    }else if(z->data<y->data){
        y->left=z;
    }else{
        y->right=z;
    }

}
void show(vector<int> B){
    for(int i=0;i<B.size();i++){
        cout<<' '<<B[i];
    }
    cout<<endl;
}

int main(){
    int n,i;
    string str;
    int num;
    node *z=NIL;
    cin>>n;
    for(i=0;i<n;i++){
        cin>>str;
        if(str=="insert"){
            cin>>num;
            z=(node *)malloc(sizeof(node));
            z->data=num;
            z->left=NIL;
            z->right=NIL;
            z->parent=NIL;
            //insertToTree(root,z);
            insertToTree(z);
        }else if(str=="find"){
            cin>>num;
            if(findNode(num))
                cout<<"yes"<<endl;
            else cout<<"no"<<endl;
        }else if(str=="delete"){
            cin>>num;
            delNode(num);
        }else if(str=="print"){
            getIn(root);
            show(InOrder);
            InOrder.clear();
            getPre(root);
            show(PreOrder);
            PreOrder.clear();
            //root=NULL;
        }
    }
}



Measured memory_bytes: 26968