#include<stdio.h>
typedef struct Node *link;
struct Node{
        int key;
        link parent;
        link left;
        link right;
    };
link insert(link,link);
link next(link);
void find(link,int);
void inorder(link);
void preorder(link);
int main(void){
    int N,i,n,now=0,count=0;
    char word[10];
    link r = NULL;
    link del;
    link L[1000];
    scanf("%d",&N);
    while(count<N){
        count++;
        scanf("%s",word);
        if(word[0] == 'p'){
            inorder(r);
            printf("\n");
            preorder(r);
            printf("\n");
            continue;
        }
        else if(word[0] == 'f'){
            scanf("%d",&n);
            find(r,n);
            continue;
        }
        else if(word[0] == 'd'){
            scanf("%d",&n);
            for(i=0;i<now;i++){
                if(n==L[i]->key){
                    del = L[i];
                    break;
                }
            }
            delete(del);
            continue;
        }
        else{
            scanf("%d",&n);
            L[now]  = malloc(sizeof(*L[now]));
            L[now]->key = n;
            r = insert(L[now],r);
            now++;
        }
    }
    return 0;
}
link insert(link z,link root){
    link x,p;
    p = NULL;
    x = root;
    while(x != NULL){
        p = x;
        if(z->key < x->key){
            x = x->left;
        }
        else{
            x = x->right;
        }
    }
    z->parent = p;
    if(p == NULL){
        root = z;
    }
    else if(z->key < p->key){
        p->left = z;
    }
    else{
        p->right = z;
    }
    return root;
}
void find(link root,int key){
    link x;
    int flag=0;
    x = root;
    while(x != NULL){
        if(key == x->key){
            flag = 1;
            break;
        }
        if(key < x->key){
            x = x->left;
        }
        else{
            x = x->right;
        }
    }
    if(flag==1) printf("yes\n");
    else printf("no\n");
}
void delete(link n){
    link x,p;
    if(n->left == NULL && n->right == NULL){
        if(n->parent->right == n){
            n->parent->right = NULL;
        }
        else if(n->parent->left == n){
            n->parent->left = NULL;
        }
        n = NULL;
    }
    else if(n->left == NULL){
        n->right->parent = n->parent;
        if(n->parent->right == n){
            n->parent->right = n->right;
        }
        else if(n->parent->left == n){
            n->parent->left = n->right;
        }
        n = NULL;
    }
    else if(n->right == NULL){
        n->left->parent = n->parent;
        if(n->parent->right == n){
            n->parent->right = n->left;
        }
        else if(n->parent->left == n){
            n->parent->left = n->left;
        }
        n = NULL;
    }
    else{
        x = next(n);
        n->key = x->key;
        delete(x);
    }
}
link next(link n){
    link x;
    x = n;
    while(x->left != NULL){
        x = x->left;
    }
    return x;
}
void inorder(link n){
    if(n == NULL) return;
    inorder(n->left);
    printf(" %d",n->key);
    inorder(n->right);
}
void preorder(link n){
    if(n == NULL) return;
    printf(" %d",n->key);
    preorder(n->left);
    preorder(n->right);
}
