#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5e5+7;
struct Node{
    int key;
    Node* parent;
    Node* left;
    Node* right;
};
Node* T;
void insert_num(int z){
    Node* s = (Node*)malloc(sizeof(Node));
    s->key = z;
    s->parent = NULL;
    s->left = NULL;
    s->right = NULL;

    Node* y = NULL; // parent of x
    Node* x = T;
    while(x != NULL){
        y = x; // set the parent
        if(z < x->key){
            x = x->left; // move to the left child
        }else{
            x = x->right; // move to the right child
        }
    }
    s->parent = y;
    if(y == NULL){
        T = s;
    } // T is empty
    else if(z < y->key){
        y->left = s;
    }// z is the left child of y
    else{
        y->right = s;
    }// z is the right child of y
}
void in(Node* T){
    if(T==NULL) return;
    in(T->left);
    printf(" %d", T->key);
    in(T->right);
}
void pre(Node* T){
    if(T==NULL) return;
    printf(" %d", T->key);
    pre(T->left);
    pre(T->right);
}
void print_nums(){
    in(T);
    printf("\n");
    pre(T);
    printf("\n");
}

bool find_num(int num){
    Node* p = T;
    while(p != NULL){
        if(p->key > num){
            p = p->left;
        }else if(p->key < num){
            p = p->right;
        }else{
            return true;
        }
    }
    return false;
}
void delete_num(int num){
    Node* p = T;
    while(p != NULL){
        if(p->key > num){
            p = p->left;
        }else if(p->key < num){
            p = p->right;
        }else{
            break;
        }
    }
    Node *u = p->parent;
    if(p->left == NULL && p->right == NULL){
        if(u->left == p) u->left = NULL;
        else u->right = NULL;
        free(p);
    }else if(p->left != NULL && p->right != NULL){
        Node *tmp = p->right;
        while(tmp->left != NULL){
            tmp = tmp -> left;
        }
        p->key = tmp->key;
        if(tmp->right == NULL){
            if((tmp->parent)->left == tmp) (tmp->parent)->left = NULL;
            else (tmp->parent)->right = NULL;
        }else{
            if((tmp->parent)->left == tmp) (tmp->parent)->left = tmp->right;
            else (tmp->parent)->right = tmp->right;
            (tmp->right)->parent = (tmp->parent);
        }
        free(tmp);
    }else{
        if(p->left!=NULL){
            if(u->left == p) u->left = p->left;
            else u->right = p->left;
            (p->left)->parent = u;
        }else{
            if(u->left == p) u->left = p->right;
            else u->right = p->right;
            (p->right)->parent = u;
        }
        free(p);
    }

}

int main(){
    T = NULL;
    int n;
    scanf("%d", &n);
    char s[10];
    for(int i = 0; i < n; i++){
        scanf("%s", s);
        if(s[0] == 'i'){
            int num;
            scanf("%d", &num);
            insert_num(num);
        } else if(s[0] == 'f'){
            int num;
            scanf("%d", &num);
            bool x = find_num(num);
            if(x == true){
                printf("yes\n");
            }else{
                printf("no\n");
            }
        } else if(s[0] == 'd'){
            int num;
            scanf("%d", &num);
            delete_num(num);
        } else{
            print_nums();
        }

    }
    return 0;
}



Measured memory_bytes: 21936