#include <cstdio>
#include <cstring>
#include <memory>

typedef struct Node {
    int key;
    std::shared_ptr<struct Node> left;
    std::shared_ptr<struct Node> right;
    Node(){};
    Node(int val){this->key = val; left = nullptr; right = nullptr;};
    ~Node(){left = nullptr; right = nullptr;};
} Node;

std::shared_ptr<Node> insert_iter(std::shared_ptr<Node> t, int v){
    if(t == nullptr){
        std::shared_ptr<Node> p(new Node(v));
        return p;
    }
    if(v < t->key) t->left = insert_iter(t->left, v);
    else if(v > t->key) t->right = insert_iter(t->right, v);
    return t;
}

void insert(std::shared_ptr<Node> *root, int v){
    *root = insert_iter(*root, v);
}

std::shared_ptr<Node> find(std::shared_ptr<Node> node, int v){
    if(node == nullptr) return nullptr;
    else if(v < node->key) return find(node->left, v);
    else if(v > node->key) return find(node->right, v);
    //else                                                                                                                                                                                                                          
    return node;
}

std::shared_ptr<Node> findAndDeleteMostLeftVal(std::shared_ptr<Node> node, int *val){
    if(node->left == nullptr){
       *val = node->key;
       return node->right;
    }
    node->left = findAndDeleteMostLeftVal(node->left, val);
    return node;
}

std::shared_ptr<Node> deleteNode(std::shared_ptr<Node> node, int v){
    if(node == nullptr) return nullptr;
    else if(v < node->key){
        node->left = deleteNode(node->left, v);
    }
    else if(v > node->key){
        node->right = deleteNode(node->right, v);
    }
    else {
        int d = 0;
        if(node->left == nullptr && node->right == nullptr) return nullptr;
        else if(node->left == nullptr) return node->right;
        else if(node->right == nullptr) return node->left;
        else{
            node->right = findAndDeleteMostLeftVal(node->right, &d);
            node->key = d;
            return node;
        }
    }
    return node;
}

void print_inorder(std::shared_ptr<Node> node){
    if(node == nullptr) return;
    print_inorder(node->left);
    std::printf(" %d", node->key);
    print_inorder(node->right);
}

void print_preorder(std::shared_ptr<Node> node){
    if(node == nullptr) return;
    std::printf(" %d", node->key);
    print_preorder(node->left);
    print_preorder(node->right);
}

int main(){
    int n;
    std::scanf("%d", &n);
    std::shared_ptr<Node> root = nullptr;
    for(int i = 0; i < n; ++i){
        char op[8];
        std::scanf("%s", op);
        if(std::strncmp(op, "insert", 5) == 0){
            int v;
            std::scanf("%d", &v);
            insert(&root, v);
        } else if(std::strncmp(op, "find", 4) == 0){
            int v;
            std::scanf("%d", &v);
            if(find(root, v) != nullptr){
                std::printf("yes\n");
            } else {
                std::printf("no\n");
            }
        } else if(std::strncmp(op, "print", 5) == 0){
            print_inorder(root);
            std::printf("\n");
            print_preorder(root);
            std::printf("\n");
        } else if(std::strncmp(op, "delete", 6) == 0){
            int v;
            std::scanf("%d", &v);
            root = deleteNode(root, v);
        }
    }

    return 0;
}



Measured memory_bytes: 34000