#include <iostream>
#include <stdio.h>
#define N_MAX 500000
using namespace std;
//thik&solve easy 
 
int n;//num of input
 
class node{//?°???\???????????????????????????????????????
public:
    int *key;
    node *parent;
    node *l_chld;
    node *r_chld;
 
    node(int n){
        key =new int(n);
         
        parent = NULL;
        l_chld = NULL;
        r_chld = NULL;
    }
 
    ~node(){
        delete key;
        delete parent;
        delete l_chld;
        delete r_chld;
    }
};
 
void insert(node **root,node *z){
    node *y;
    node *x;
    y = NULL;
    x = *root;
    while(x != NULL){
        y = x;
        if(*(z->key) < *(x->key))
            x = x->l_chld;
        else
            x = x->r_chld;
        z->parent = y;
    }
 
    if(y == NULL)
        *root = z;
    else if(*(z->key) < *(y->key))
        y->l_chld = z;
    else
        y->r_chld = z;
     
}
 
void preorder(node* u){
    node* pos;
    cout <<" " <<*(u->key);
 
    if(u->l_chld != NULL){
        pos = u->l_chld;
        preorder(pos);
    }
 
    if(u->r_chld != NULL){
        pos = u->r_chld;
        preorder(pos);
    }
}
 
void inorder(node* u){
    node* pos;
     
    if(u->l_chld != NULL){
        pos = u->l_chld;
        inorder(pos);
    }
    cout <<" " <<*(u->key);
 
    if(u->r_chld != NULL){
        pos = u->r_chld;
        inorder(pos);
    }
}
 
void print(node *u){
    inorder(u);
    cout <<endl;
 
    preorder(u);
    cout <<endl;
 
}
 
bool find(node* u,int k){
    node* pos;
    bool b;
    if(k == *(u->key)) return true;
 
    if(k < *(u->key)){
        if(u->l_chld == NULL) return false;//?????´????????¨????????°????????????????????????????????????
        pos = u->l_chld;
        b = find(pos,k);
    }else{
        if(u->r_chld == NULL) return false;//?????????
        pos = u->r_chld;
        b = find(pos,k);
    }
    return b;
}
 
node* search(node* u,int k){
    node* pos;
    node* b;
    if(k == *(u->key)) return u;
 
    if(k < *(u->key)){
        if(u->l_chld == NULL) return NULL;//?????´????????¨????????°????????????????????????????????????
        pos = u->l_chld;
        b = search(pos,k);
    }else{
        if(u->r_chld == NULL) return NULL;//?????????
        pos = u->r_chld;
        b = search(pos,k);
    }
    return b;
}
 
void delete_node(node* u,int k){
    node* z;
    node* nd_tmp;
    node* y;
    z = search(u,k);
 
    if(z == NULL){
        cout <<"no such key" <<endl;
        return;
    }
 
    if(z->l_chld == NULL && z->r_chld == NULL){//z?????????????????????
        nd_tmp = z->parent;
        if(z == nd_tmp->l_chld) nd_tmp->l_chld =NULL;
        else nd_tmp->r_chld = NULL;
        //delete z;
    }
    else if(z->l_chld != NULL && z->r_chld != NULL){//z?????????????????????
        y = z->r_chld;
        while(y->l_chld != NULL){
            y = y->l_chld;
        }
 
        z->key = y->key;
 
        nd_tmp = y->parent;
        if(y == nd_tmp->l_chld) nd_tmp->l_chld =NULL;
        else nd_tmp->r_chld = NULL;
        //delete y;
    }
    else{//z???????????????????????????
        if(z->l_chld != NULL){//z?????????????????????
            (z->l_chld)->parent = z->parent;
 
            if(z == z->parent->l_chld) z->parent->l_chld = z->l_chld;
            else z->parent->r_chld = z->l_chld;
 
            //delete z;
        }
        else{//z?????????????????????
            z->r_chld->parent = z->parent;
 
            if(z == z->parent->l_chld) z->parent->l_chld = z->r_chld;
            else z->parent->r_chld = z->r_chld;
 
            //delete z;
        }
    }
    return;//????????????????????????????????°?????£??????????????§?????????????????????
}
 
int main(){
    char type[100];
    int z;
    node* root;
    root = NULL;
 
    node** rtroot = &root;
 
    scanf("%d",&n);
     
    for(int i=0;i<n;i++){
        scanf("%s",&type);
        if(type[0] == 'i'){
            scanf("%d",&z);
 
            //?????????????????????(????????????nd?????????(=????????????)???for???????????¨)
            node* nd;
            //??°???????????\?????????????????§?????????????????????nd?????????????????????
            nd = new node(z);
 
            insert(rtroot,nd);
        }else if(type[0] == 'p'){
            print(root);
        }else if(type[0] == 'f'){
            scanf("%d",&z);
 
            if(find(root,z))cout <<"yes" <<endl;
            else cout <<"no" <<endl;
        }else if(type[0] == 'd'){
            scanf("%d",&z);
 
            delete_node(root,z);
        }else{
            cout <<"error type" <<endl;
        }
    }
 
    return 0;
}

Measured memory_bytes: 32468