#include <iostream>
#include <vector>

struct node {
    int value;
    node *left = nullptr, *right = nullptr, *parent = nullptr;
    node(int x) { value = x; }
};

struct binary_search_tree {
    node* root = nullptr;
    int size = 0;
    binary_search_tree() {}
    void insert(int v) {
        size++;
        node* c = nullptr;  //子
        node* p = root;     //親
        while (p != nullptr) {
            c = p;
            if (v < p->value) {
                p = p->left;
            } else {
                p = p->right;
            }
        }
        if (c == nullptr) {
            root = new node(v);
        } else if (v < c->value) {
            c->left = new node(v);
            c->left->parent = c;
        } else {
            c->right = new node(v);
            c->right->parent = c;
        }
    }
    node* find(int v) {
        node* x = root;
        while (x != nullptr) {
            if (v < x->value) {
                x = x->left;
            } else if (v > x->value) {
                x = x->right;
            } else {
                return x;
            }
        }
        return nullptr;
    }
    void del(node* x) {
        node* p = x->parent;
        node* l = x->left;
        node* r = x->right;
        if (l == nullptr && r == nullptr) {
            if (p != nullptr) {
                if (p->left == x) {
                    p->left = nullptr;
                } else {
                    p->right = nullptr;
                }
            }
            delete x;
        } else if (l == nullptr || r == nullptr) {
            node* lr;
            if (l == nullptr) {
                lr = r;
            } else {
                lr = l;
            }
            if (p != nullptr) {
                if (p->left == x) {
                    p->left = lr;
                } else {
                    p->right = lr;
                }
            }
            lr->parent = p;
            delete x;
        } else {
            node* y = r;
            while (true) {
                if (y->left != nullptr) {
                    y = y->left;
                } else {
                    break;
                }
            }
            x->value = y->value;
            del(y);
        }
    }
    void del(int v) { del(find(v)); }
    void print_inorder(node* now) {
        if (now->left != nullptr) { print_inorder(now->left); }
        std::cout << " " << now->value;
        if (now->right != nullptr) { print_inorder(now->right); }
    }
    void print_preorder(node* now) {
        std::cout << " " << (*now).value;
        if (now->left != nullptr) { print_preorder(now->left); }
        if (now->right != nullptr) { print_preorder(now->right); }
    }
    void print() {
        print_inorder(root);
        std::cout << std::endl;
        print_preorder(root);
        std::cout << std::endl;
    }
    void destruct(node* now) {
        if ((*now).left != nullptr) { destruct((*now).left); }
        if ((*now).right != nullptr) { destruct((*now).right); }
        delete now;
    }
    ~binary_search_tree() { destruct(root); }
};

int main() {
    int n;
    binary_search_tree bst;
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::string s;
        std::cin >> s;
        if (s == "insert") {
            int x;
            std::cin >> x;
            bst.insert(x);
        } else if (s == "find") {
            int x;
            std::cin >> x;
            if (bst.find(x) == nullptr) {
                std::cout << "no" << std::endl;
            } else {
                std::cout << "yes" << std::endl;
            }
        } else if (s == "delete") {
            int x;
            std::cin >> x;
            bst.del(x);
        } else {
            bst.print();
        }
    }
}


Measured memory_bytes: 21884