#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
struct node{
    int key;
    node* parent,*left,*right;
};
node *root=NULL;
node * pre;
void Insert(int key){
    node * temp=new node;
    temp->key=key;
    temp->left=NULL;
    temp->right=NULL;

    if(root==NULL){
        root=temp;
        return ;
    }
    else{
        pre=root;
        while(1){
            if (key<pre->key){
                if (pre->left==NULL){
                    pre->left=temp;
                    temp->parent=pre;
                    return;
                }
                else
                    pre=pre->left;
            }
            else if(key > pre->key){
                if (pre->right==NULL){
                    pre->right=temp;
                    temp->parent=pre;
                    return;
                }
                else
                    pre=pre->right;
            }
            else
                return;
        }
    }
}
void Print_in(node* r){
    if(r->left!=NULL){
        Print_in(r->left);
    }
    cout<<" "<<r->key;
    if(r->right!=NULL){
        Print_in(r->right);
    }

}
void Print_pre(node *r){
    cout<<" "<<r->key;
    if(r->left!=NULL){
        Print_pre(r->left);
    }
    if(r->right!=NULL){
        Print_pre(r->right);
    }
}
void Print(node * r){
    Print_in(r);
    cout<<endl;
    Print_pre(r);
    cout<<endl;
}
int Find(node *r,int key){
        pre=r;
        while(1){
            if (key<pre->key){
                if (pre->left==NULL){
                    return 0;
                }
                else
                    pre=pre->left;
            }
            else if(key > pre->key){
                if (pre->right==NULL){
                    return 0;
                }
                else
                    pre=pre->right;
            }
            else
                return 1;
        }
}
void Delete(int key){
        pre=root;
        node* K;
        while(1){
            if (key<pre->key){
                if (pre->left==NULL){
                    return ;
                }
                else
                    pre=pre->left;
            }
            else if(key > pre->key){
                if (pre->right==NULL){
                    return ;
                }
                else
                    pre=pre->right;
            }
            else{
                K=pre;
                break;
            }
        }
        if((K->left==NULL)&&(K->right==NULL)){
            if ((K->parent->left)==K){
                K->parent->left=NULL;
                free(K);
            }
            else{
                K->parent->right=NULL;
                free(K);
            }
            return;
        }
        else if(K->left==NULL&&K->right!=NULL){
            if ((K->parent->left)==K){
                K->parent->left=K->right;
                K->right->parent=K->parent;
                free(K);
            }
            else{
                K->parent->right=K->right;
                K->right->parent=K->parent;
                free(K);
            }
            return ;
        }
        else if(K->left!=NULL&&K->right==NULL){
            if ((K->parent->left)==K){
                K->parent->left=K->left;
                K->left->parent=K->parent;
                free(K);
            }
            else{
                K->parent->right=K->left;
                K->left->parent=K->parent;
                free(K);
            }
            return ;
        }
        else{
                node* d=K->right;
                while(d->left!=NULL){
                    d=d->left;
                }
                K->key=d->key;
                //delete d
                if(d->right!=NULL){
                    if(d->parent->left==d){
                    d->parent->left=d->right;
                    d->right->parent=d->parent;
                    free(d);
                    }
                    else {
                        d->parent->right=d->right;
                        d->right->parent=d->parent;
                        free(d);
                    }
                }
                else{
                    if(d->parent->left==d){
                    d->parent->left=NULL;
                    free(d);
                    }
                    else{
                       d->parent->right=NULL;
                       free(d);
                    }
                }
                return ;
        }
}
int main(){
    int n;
    string cmd;
    int key;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>cmd;
        if(cmd[0]=='i'){
            cin>>key;
            Insert(key);
        }
        else if(cmd[0]=='p'){
            Print(root);
        }
        else if(cmd[0]=='f'){
            cin>>key;
            int re=Find(root,key);
            if(re==0)
                cout<<"no"<<endl;
            else
                cout<<"yes"<<endl;
        }
        else if(cmd[0]=='d') {
            cin>>key;
            Delete(key);
        }
    }
    return 0;
}



Measured memory_bytes: 21916