#include<stdio.h>
#include<stdlib.h>

struct Node {
    int key;
    struct Node *right, *left, *parent;
};

struct Node *root, *NIL;

void insert(int k){
    struct Node *y = NIL;
    struct Node *x = root;
    struct Node *z;

    z = (struct Node *)malloc(sizeof(struct Node));
    z->key = k;
    z->left = NIL;
    z->right = NIL;
    

    while(x!= NIL){
        y = x;
        if(z->key < x->key){
            x = x->left;
        } else{
            x = x->right;
        }
    }
    
    z->parent = y;
    if(y == NIL){
        root = z;
    } else{
        if( z->key < y->key){
            y->left = z;
        } else{
            y->right = z;
        }
    }

}

void find(int key){
    struct Node *x = root;

    while(x != NIL){
        if(x->key > key){
            x = x->left;
        } else if(x->key < key) {
            x = x->right;
        } else{
            printf("yes\n");
            return;
        }
    }

    printf("no\n");
}


void delete_no_child(struct Node *u, int key){
    if(u->parent != NIL){
        if(u->parent->left != NIL && u->parent->left->key == key) u->parent->left=NIL;
        else if(u->parent->right != NIL && u->parent->right->key == key) u->parent->right=NIL;
    } else{
        root = NIL;
    }
}

void delete_one_child(struct Node *u, int key){
    if(u->parent->left != NIL && u->parent->left->key == key){
        if(u->left == NIL) {
            u->parent->left = u->right;
            u->right->parent = u->parent;
        }else{ 
            u->parent->left = u->left;
            u->left->parent = u->parent;
        }
    } else if(u->parent->right != NIL && u->parent->right->key == key){
        if(u->left == NIL) {
            u->parent->right = u->right;
            u->right->parent = u->parent;
        }else{ 
            u->parent->right = u->left; 
            u->left->parent = u->parent;
        }
    }
    
}

void delete_two_children(struct Node *u, int key){
    u->key = u->right->key;
    if(u->right->left == NIL && u->right->right == NIL){
        delete_no_child(u->right, u->right->key);
    } else if(u->right->left != NIL && u->right->right != NIL){
        delete_two_children(u->right, u->right->key);
    } else{
        delete_one_child(u->right, u->right->key);
    }
}

void delete(int key){
    struct Node *x = root;
    struct Node *y = NIL;
    while(x != NIL){
        /* xがNILの一個前のNodeが欲しい */
        y = x;
        if(x->key > key){
            x = x->left;
        } else if(x->key < key) {
            x = x->right;
        } else{
            break;
        }
    }

    if(y->left == NIL && y->right == NIL){
        delete_no_child(y, key);
    } else if(y->left != NIL && y->right != NIL){
        delete_two_children(y, key);
    } else{
        delete_one_child(y, key);
    }
}


void inorder(struct Node *u){
    if(u == NIL) return;
    inorder(u->left);
    printf(" %d", u->key);
    inorder(u->right);
}

void preorder(struct Node *u){
    if(u == NIL) return;
    printf(" %d", u->key);
    preorder(u->left);
    preorder(u->right);
}

int main(){
    int n, i, x;
    char com[10];
    scanf("%d", &n);

    for(i = 0; i < n; i++){
        scanf("%s", com);
        if(com[0] == 'i'){
            scanf("%d", &x);
            insert(x);
        } else if(com[0] == 'f'){
            scanf("%d", &x);
            find(x);
        } else if(com[0] == 'd'){
            scanf("%d", &x);
            delete(x);
        } else{
            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
        }
    }

    return 0;
}
