#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;
// Welcome to my source code!

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

Node *root, *NIL;

void insertKey(int key) {
    Node *y = NIL;
    Node *x = root;
    Node *z;

    z = new Node;
    z->key = key;
    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;
        }
    }
}

Node* findKey(Node *u, int key) {
    while (u != NIL && u->key != key) {
        if (key < u->key) {
            u = u->left;
        } else {
            u = u->right;
        }
    }
    return u;
}

Node* getMinimum(Node *x) {
    while (x->left != NIL) x = x->left;
    return x;
}

Node* getSuccessor(Node *z) {
    if (z->right != NIL) return getMinimum(z->right);
    Node *y = z->parent;
    while (y != NIL && z == y->right) {
        z = y;
        y = y->parent;
    }
    return y;
}

Node* deleteNode(Node *z) {
    Node *y;
    Node *x;

    if (z->left == NIL || z->right == NIL) y = z;
    else y = getSuccessor(z);

    if (y->left != NIL) x = y->left;
    else x = y->right;

    if (x != NIL) x->parent = y->parent;

    if (y->parent == NIL) root = x;
    else if (y == y->parent->left) y->parent->left = x;
    else y->parent->right = x;

    if (y != z) z->key = y->key;
}

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

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

int main() {
    int m, k;
    string com;
    cin >> m;
    
    REP(i,m) {
        cin >> com;
        if (com == "print") {
            inorder(root);
            cout << '\n';
            preorder(root);
            cout << endl;
        } else {
            cin >> k;
            if (com == "insert") insertKey(k);
            else if (com == "find") {
                if (findKey(root, k) != NIL) cout << "yes" << endl;
                else cout << "no" << endl;
            } else {
                deleteNode(findKey(root, k));
            }
        }
    }
}

#include <stdio.h>
struct binary_tree{
    int n;
    int l;
    int r;
};
binary_tree bt[1000000];
int now=0,empty[1000000];
void push(int n,int t){
    if(now==0){
        bt[0].n=n;
        now++;
        return;
    }
    if(bt[t].n<n){
        if(bt[t].r==0){
            bt[t].r=empty[now];
            bt[bt[t].r].n=n;
            now++;
            return;
        }
        else push(n,bt[t].r);
    }
    if(bt[t].n>n){
        if(bt[t].l==0){
            bt[t].l=empty[now];
            bt[bt[t].l].n=n;
            now++;
            return;
        }
        else push(n,bt[t].l);
    }
}
void dfs(int n){
    printf(" %d",bt[n].n);
    if(bt[n].l!=0){
        dfs(bt[n].l);
    }
    if(bt[n].r!=0){
        dfs(bt[n].r);
    }
}
void print_sorted(int n){
    if(bt[n].l!=0){
        print_sorted(bt[n].l);
    }
    printf(" %d",bt[n].n);
    if(bt[n].r!=0){
        print_sorted(bt[n].r);
    }
}
bool find(int n,int t){
    if(bt[t].n==n)return true;
    if(bt[t].n<n){
        if(bt[t].r!=0)return find(n,bt[t].r);
        return false;
    }
    if(bt[t].l!=0)return find(n,bt[t].l);
    return false;
}
void delete_item(int n,int t,int p){
    if(bt[t].n==n){
        if(bt[t].r!=0&&bt[t].l!=0){
            int big=bt[t].r,bp=t;
            while(bt[big].l!=0){
                bp=big;
                big=bt[big].l;
            }
            bt[t].n=bt[big].n;
            if(bp!=t)bt[bp].l=bt[big].r;
            else bt[bp].r=bt[big].r;
            now--;
            empty[now]=big;
            bt[big].n=bt[big].l=bt[big].r=0;
            return;
        }
        if(bt[t].r==0){
            if(p==-1){
                now--;
                empty[now]=bt[t].l;
                bt[t]=bt[bt[t].l];
                bt[empty[now]].n=bt[empty[now]].l=bt[empty[now]].r=0;
                return;
            }
            if(bt[p].n>n)bt[p].l=bt[t].l;
            else bt[p].r=bt[t].l;
            now--;
            empty[now]=t;
            bt[empty[now]].n=bt[empty[now]].l=bt[empty[now]].r=0;
            return;
        }
        if(bt[t].l==0){
            if(p==-1){
                now--;
                empty[now]=bt[t].r;
                bt[t]=bt[bt[t].r];
                bt[empty[now]].n=bt[empty[now]].l=bt[empty[now]].r=0;
                return;
            }
            if(bt[p].n>n)bt[p].l=bt[t].r;
            else bt[p].r=bt[t].r;
            now--;
            empty[now]=t;
            bt[empty[now]].n=bt[empty[now]].l=bt[empty[now]].r=0;
            return;
        }
    }
    else if(bt[t].n>n){
        delete_item(n,bt[t].l,t);
        return;
    }
    else delete_item(n,bt[t].r,t);
}
int main(){
    int n,temp;
    char a[1000];
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        empty[i]=i;
        bt[i].n=bt[i].l=bt[i].r=0;
        scanf("%s",a);
        if(a[0]=='i'){
            scanf("%d",&temp);
            push(temp,0);
        }
        if(a[0]=='p'){
            print_sorted(0);
            printf("\n");
            dfs(0);
            printf("\n");
        }
        if(a[0]=='f'){
            scanf("%d",&temp);
            if(find(temp,0))printf("yes\n");
            else printf("no\n");
        }
        if(a[0]=='d'){
            scanf("%d",&temp);
            delete_item(temp,0,-1);
        }
    }
}