#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;
    }
    void set_left(size_t p, size_t q) {
        nodes[p].left = q;
    }
    void set_right(size_t p, size_t q) {
        nodes[p].right = q;
    }
    void set_parent(size_t p, size_t q) {
        nodes[p].parent = q;
    }
    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;
            x = (v<get_value(x)? get_left(x):get_right(x));
        }
        nodes.back().parent = y;

        if (!~y) {
            root = 0;
        } else if (v < get_value(y)) {
            set_left(y, nodes.size()-1);
        } else {
            set_right(y, nodes.size()-1);
        }
    }
    size_t find(T v, size_t x=0) {
        size_t l=get_left(x);
        if (~l && v<get_value(x)) {
            size_t res=find(v, l);
            if (~res) return res;
        }

        size_t r=get_right(x);
        if (~r && get_value(x)<v) {
            size_t res=find(v, r);
            if (~res) return res;
        }

        return get_value(x)==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, get_left(x));
        F(nodes[x]);
        walk_inorder(F, get_right(x));
    }
    template <class Functor>
    void walk_preorder(Functor F, size_t x=0) {
        if (!~x) return;

        F(nodes[x]);
        walk_preorder(F, get_left(x));
        walk_preorder(F, get_right(x));
    }
};

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: 19032