#include<bits/stdc++.h>

using namespace std;

typedef struct node_{
    int id;
    node_* left;
    node_* right;
    node_* mother;
}* node;

void printPreorder(node root){
    printf(" %d", root -> id);
    if(root -> left != NULL){
        printPreorder(root -> left);
    }
    if(root -> right != NULL){
        printPreorder(root -> right);
    }
}

void printInorder(node root){
    if(root -> left != NULL){
        printInorder(root -> left);
    }
    printf(" %d", root -> id);
    if(root -> right != NULL){
        printInorder(root -> right);
    }
}

void printTree(node root){
    if(root == NULL){
        printf("\n\n");
    }else{
        printInorder(root);
        printf("\n");
        printPreorder(root);
        printf("\n");
    }
}

void nodeInsert(node root, node nd){
    if(root -> id > nd -> id){
        if(root -> left == NULL){
            root -> left = nd;
            nd -> mother = root;
        }else{
            nodeInsert(root -> left, nd);
        }
    }else{
        if(root -> right == NULL){
            root -> right = nd;
            nd -> mother = root;
        }else{
            nodeInsert(root -> right, nd);
        }
    }
    
}

int findNode(node root, int key){
    if(key == root -> id){
        return 1;
    }else if(key < root -> id && root -> left != NULL){
        int ans = findNode(root -> left, key);
        return(ans);
    }else if(key > root -> id && root -> right != NULL){
        int ans = findNode(root -> right, key);
        return ans;
    }else{
        return 0;
    }
    return 0;
}

node jisetten(node root){
    node p = root -> right;
    while(p -> left != NULL){
        p = p -> left;
    }
    return p;
}

void deleteNode(node root, int key){
    if(key == root -> id){
        if(root -> left == NULL && root -> right == NULL){
            if(root -> id < root -> mother -> id){
                root -> mother -> left = NULL;
            }else{
                root -> mother -> right = NULL;
            }
        }else if(root -> left == NULL){
            root -> right -> mother = root -> mother;
            if(root -> id < root -> mother -> id){
                root -> mother -> left = root -> right;
            }else{
                root -> mother -> right = root -> right;
            }
        }else if(root -> right == NULL){
            root -> left -> mother = root -> mother;
            if(root -> id < root -> mother -> id){
                root -> mother -> left = root -> left;
            }else{
                root -> mother -> right = root -> left;
            }
        }else{
            //2つ子供ある場合の処理
            node p = jisetten(root);
            root -> id = p -> id;
            if(p -> left == NULL && p -> right == NULL){
                if(p -> id < p -> mother -> id){
                    p -> mother -> left = NULL;
                }else{
                    p -> mother -> right = NULL;
                }
            }else if(p -> left == NULL){
                p -> right -> mother = p -> mother;
                if(p -> id < p -> mother -> id){
                    p -> mother -> left = p -> right;
                }else{
                    p -> mother -> right = p -> right;
                }
            }else if(p -> right == NULL){
                p -> left -> mother = p -> mother;
                if(p -> id < p -> mother -> id){
                    p -> mother -> left = p -> left;
                }else{
                    p -> mother -> right = p -> left;
                }
            }
        }
    }else if(key < root -> id && root -> left != NULL){
        deleteNode(root -> left, key);
    }else if(key > root -> id && root -> right != NULL){
        deleteNode(root -> right, key);
    }else{
        return;
    }
}

int main(){
    int n;
    scanf("%d", &n);
    char* option = new char[7];
    node root = NULL;
    for(int i = 0; i < n; i++){
        scanf("%s", option);
        if(strcmp(option, "insert") == 0){
            int id;
            scanf("%d", &id);
            node nd = new node_;
            nd -> id = id;
            nd -> left = NULL;
            nd -> right = NULL;
            if(root == NULL){
                root = nd;    
            }else{
                nodeInsert(root, nd);
            }
        }else if(strcmp(option, "print") == 0){
            printTree(root);
        }else if(strcmp(option, "find") == 0){
            int key;
            scanf("%d", &key);
            if(findNode(root, key) == 1){
                printf("yes\n");
            }else{
                printf("no\n");
            }
        }else{
            int key;
            scanf("%d", &key);
            deleteNode(root, key);
        }
    }
    return 0;
}


Measured memory_bytes: 21932