#include<stdio.h>

using namespace std;

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

Node* NIL;
Node* root;

void inorder(Node* current){
    if(current == NIL){
        return;
    }

    inorder(current->left);
    printf(" %d", current->key);
    inorder(current->right);
}

void preorder(Node* current){
    if(current == NIL){
        return;
    }

    printf(" %d", current->key);
    preorder(current->left);
    preorder(current->right);
}

bool find(int key){
    bool flag = false;
    Node* current = root;
    while(current != NIL){
        if(key < current->key){
            current = current->left;
        }
        else if(key > current->key){
            current = current->right;
        }
        else{
            flag = true;
            break;
        }
    }
    return flag;
}

Node* getsuccessor(Node* current){
    if(current->left == NIL){
        return current;
    }
    return getsuccessor(current->left);
}

void deletenode(Node* current){
    Node* parent = current->parent;

    bool leftflag = true;
    if(parent->left != current){
        leftflag = false;
    }

    if(current->left == NIL && current->right == NIL){
        if(leftflag){
            parent->left = NIL;
        }
        else{
            parent->right = NIL;
        }
        delete current;
    }
    else if(current->left != NIL && current->right == NIL){
        if(leftflag){
            parent->left = current->left;
        }
        else{
            parent->right = current->left;
        }
        current->left->parent = parent;
        delete current;
    }
    else if(current->left == NIL && current->right != NIL){
        if(leftflag){
            parent->left = current->right;
        }
        else{
            parent->right = current->right;
        }
        current->right->parent = parent;
        delete current;
    }
    else{
        Node* p = getsuccessor(current->right);
        current->key = p->key;
        deletenode(p);
    }
}

int main(){
    int n;
    scanf("%d", &n);

    for(int i = 0; i < n; i++){
        char str[7];
        scanf("%s", str);

        if(str[0] == 'i'){
            Node* z = new Node;
            scanf("%d", &z->key);
            z->left = NIL;
            z->right = NIL;

            Node* y = NIL;
            Node* x = root;
            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;
            }
        }
        else if(str[0] == 'p'){
            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
        }
        else if(str[0] == 'f'){
            int key;
            scanf("%d", &key);
            if(find(key)){
                printf("yes\n");
            }
            else{
                printf("no\n");
            }
        }
        else{
            int key;
            scanf("%d", &key);
            if(!find(key)){
                continue;
            }
            else{
                Node* z = root;
                while(key != z->key){
                    if(key < z->key){
                        z = z->left;
                    }
                    else{
                        z = z->right;
                    }
                }
                deletenode(z);
            }
        }
    }
}


Measured memory_bytes: 21324