#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

template <class T>
struct Node {
    T value;
    size_t parent, left, right;
    Node(): value(), parent(-1), left(-1), right(-1) {}
    Node(T v): value(v), parent(-1), left(-1), right(-1) {}
};

template <class T>
struct BinarySearchTree {
    vector<Node<T>> nodes;
    size_t root;
    BinarySearchTree(): root(-1) {}
    size_t &get_left(size_t p) {
        return nodes[p].left;
    }
    size_t &get_right(size_t p) {
        return nodes[p].right;
    }
    size_t &get_parent(size_t p) {
        return nodes[p].parent;
    }
    size_t &get_child(size_t p) {
        return ~get_left(p)? get_left(p):get_right(p);
    }
    T get_value(size_t p) {
        return ~p? nodes[p].value:T(-2);
        return nodes[p].value;
    }
    size_t num_child(size_t p) {
        return !!~get_left(p) + !!~get_right(p);
    }
    void insert(T v) {
        Node<T> z(v);
        nodes.push_back(z);

        size_t x=root, y=-1;
        while (~x) {
            y = x;
            if (v < nodes[x].value) {
                x = nodes[x].left;
            } else {
                x = nodes[x].right;
            }
        }

        nodes.back().parent = y;

        if (!~y) {
            root = 0;
        } else if (v < nodes[y].value) {
            nodes[y].left = nodes.size()-1;
        } else {
            nodes[y].right = nodes.size()-1;
        }
    }
    size_t find(T v, size_t x=0) {
        size_t l=nodes[x].left;
        if (~l && nodes[x].value>v) {
            size_t res=find(v, l);
            if (~res) return res;
        }

        size_t r=nodes[x].right;
        if (~r && nodes[x].value<v) {
            size_t res=find(v, r);
            if (~res) return res;
        }

        return nodes[x].value==v? x:-1;
    }
    void remove(T v, size_t z) {
        if (num_child(z) == 0) {
            if (get_left(get_parent(z)) == z) {
                get_left(get_parent(z)) = -1;
            } else {
                get_right(get_parent(z)) = -1;
            }
            get_parent(z) = -1;
        } else if (num_child(z) == 1) {
            get_parent(get_child(z)) = get_parent(z);
            if (get_left(get_parent(z)) == z) {
                get_left(get_parent(z)) = get_child(z);
            } else {
                get_right(get_parent(z)) = get_child(z);
            }
        } else {
            size_t y;
            for (y=get_right(z); ~get_left(y); y=get_left(y));

            nodes[z].value = get_value(y);
            remove(get_value(y), y);
        }
    }
    bool remove(T v) {
        size_t z=find(v);
        if (!~z) return false;

        remove(v, z);
        return true;
    }
    template <class Functor>
    void walk_inorder(Functor F, size_t x=0) {
        if (!~x) return;

        walk_inorder(F, nodes[x].left);
        F(nodes[x]);
        walk_inorder(F, nodes[x].right);
    }
    template <class Functor>
    void walk_preorder(Functor F, size_t x=0) {
        if (!~x) return;

        F(nodes[x]);
        walk_preorder(F, nodes[x].left);
        walk_preorder(F, nodes[x].right);
    }
};

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

    auto output=[](Node<int> &x)->void {
        printf(" %d", x.value);
    };

    BinarySearchTree<int> tree;
    for (size_t i=0; i<n; ++i) {
        char buf[1<<3];
        scanf("%s", buf);
        string instr(buf);

        if (instr == "insert") {
            int key;
            scanf("%d", &key);
            tree.insert(key);
        } else if (instr == "print") {
            tree.walk_inorder(output);
            printf("\n");
            tree.walk_preorder(output);
            printf("\n");
        } else if (instr == "find") {
            int key;
            scanf("%d", &key);
            printf("%s\n", ~tree.find(key)? "yes":"no");
        } else if (instr == "delete") {
            int key;
            scanf("%d", &key);
            tree.remove(key);
        }
    }

    return 0;
}

Measured memory_bytes: 19100