#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

class Node
{
public:
    Node() {
        this->_val = 0;
        this->_parent = -1;
        this->_left = -1;
        this->_right = -1;
    }
    Node(int val) {
        this->_val = val;
        this->_parent = -1;
        this->_left = -1;
        this->_right = -1;
    };
    ~Node(){
        ;
    };

    const int& val() const { return this->_val; }
    int& val() { return this->_val; }

    const int& parent() const { return this->_parent; }
    int& parent() { return this->_parent; }

    const int& left() const { return this->_left; }
    int& left() { return this->_left; }

    const int& right() const { return this->_right; }
    int& right() { return this->_right; }

private:
    int _val;
    int _parent;
    int _left;
    int _right;
};

std::vector<Node> nodelist(500000);
const int rootid = 0;
int counter = 0;

void insert(int newval)
{
    if (counter == 0) {
        nodelist[rootid].val() = newval;
        return;
    }

    int y; // ?????¨??°????????????????????????
    int x = rootid; // ?????¨??°??????????????????
    while (x != -1) {
        y = x;
        if (newval < nodelist[x].val()) {
            x = nodelist[x].left(); // ?????????????§????
        } else {
            x = nodelist[x].right();
        }
    }
    nodelist[counter].parent() = y;
    nodelist[counter].val() = newval;

    if (newval < nodelist[y].val()) {
        nodelist[y].left() = counter; // ?????????????????????
    } else {
        nodelist[y].right() = counter; // ?????????????????????
    }
}

void find(int val, int curid)
{
    if (nodelist[curid].val() == val) {
        cout << "yes" << endl;
        return;
    } else {
        if (nodelist[curid].left() == -1 && nodelist[curid].right() == -1) {
            cout << "no" << endl;
            return;
        } else if (val < nodelist[curid].val()) {
            if (nodelist[curid].left() == -1) {
                cout << "no" << endl;
                return;
            } else {
                find(val, nodelist[curid].left());
            }
        } else {
            if (nodelist[curid].right() == -1) {
                cout << "no" << endl;
                return;
            } else {
                find(val, nodelist[curid].right());
            }
        }
    }
}

int findmax(int curid, int curmaxid)
{
    if (nodelist[curid].left() == -1 && nodelist[curid].right() == -1) {
        if (nodelist[curid].val() > nodelist[curmaxid].val()) {
            return curid;
        } else {
            return curmaxid;
        }
    } else if (nodelist[curid].left() == -1) {
        if (nodelist[curid].val() > nodelist[curmaxid].val()) {
            return findmax(nodelist[curid].right(), curid);
        } else {
            return findmax(nodelist[curid].right(), curmaxid);
        }
    } else if (nodelist[curid].right() == -1) {
        if (nodelist[curid].val() > nodelist[curmaxid].val()) {
            return findmax(nodelist[curid].left(), curid);
        } else {
            return findmax(nodelist[curid].left(), curmaxid);
        }
    } else {
        if (nodelist[curid].val() > nodelist[curmaxid].val()) {
            int leftmax = findmax(nodelist[curid].left(), curid);
            int rightmax = findmax(nodelist[curid].right(), curid);
            if (nodelist[leftmax].val() > nodelist[rightmax].val()) {
                return leftmax;
            } else {
                return rightmax;
            }
        } else {
            int leftmax = findmax(nodelist[curid].left(), curmaxid);
            int rightmax = findmax(nodelist[curid].right(), curmaxid);
            if (nodelist[leftmax].val() > nodelist[rightmax].val()) {
                return leftmax;
            } else {
                return rightmax;
            }
        }
    }
}

void deletenode(int val, int curid)
{
    if (nodelist[curid].val() == val) {
        if (nodelist[curid].left() == -1 && nodelist[curid].right() == -1) {
            if (nodelist[nodelist[curid].parent()].left() == curid) {
                nodelist[nodelist[curid].parent()].left() = -1;
            } else {
                nodelist[nodelist[curid].parent()].right() = -1;
            }
        } else if (nodelist[curid].left() == -1) {
            if (nodelist[nodelist[curid].parent()].left() == curid) {
                nodelist[nodelist[curid].parent()].left() = nodelist[curid].right();
                nodelist[nodelist[curid].right()].parent() = nodelist[curid].parent();
            } else {
                nodelist[nodelist[curid].parent()].right() = nodelist[curid].right();
                nodelist[nodelist[curid].right()].parent() = nodelist[curid].parent();
            }
        } else if (nodelist[curid].right() == -1) {
            if (nodelist[nodelist[curid].parent()].left() == curid) {
                nodelist[nodelist[curid].parent()].left() = nodelist[curid].left();
                nodelist[nodelist[curid].left()].parent() = nodelist[curid].parent();
            } else {
                nodelist[nodelist[curid].parent()].right() = nodelist[curid].left();
                nodelist[nodelist[curid].left()].parent() = nodelist[curid].parent();
            }
        } else {
            int rightmin = nodelist[curid].right();
            while (nodelist[rightmin].left() != -1) {
                rightmin = nodelist[rightmin].left();
            }
            nodelist[curid].val() = nodelist[rightmin].val();
            deletenode(nodelist[rightmin].val(), rightmin);
        }
    } else {
        if (nodelist[curid].left() == -1 && nodelist[curid].right() == -1) {
            return;
        } else if (nodelist[curid].left() == -1) {
            deletenode(val, nodelist[curid].right());
        } else if (nodelist[curid].right() == -1) {
            deletenode(val, nodelist[curid].left());
        } else {
            deletenode(val, nodelist[curid].left());
            deletenode(val, nodelist[curid].right());
        }
    }
}

void inorder(int id)
{
    if (nodelist[id].left() != -1) {
        inorder(nodelist[id].left());
    }
    cout << " " << nodelist[id].val();
    if (nodelist[id].right() != -1) {
        inorder(nodelist[id].right());
    }
}

void preorder(int id)
{
    cout << " " << nodelist[id].val();
    if (nodelist[id].left() != -1) {
        preorder(nodelist[id].left());
    }
    if (nodelist[id].right() != -1) {
        preorder(nodelist[id].right());
    }
}

int main()
{
    int n;
    cin >> n;
    std::string op;
    int k;
    for (int i = 0; i < n; i++) {
        cin >> op;
        if (op == "insert") {
            cin >> k;
            insert(k);
            counter++;
        } else if (op == "find") {
            cin >> k;
            find(k, rootid);
        } else if (op == "delete") {
            cin >> k;
            deletenode(k, rootid);
        } else if (op == "print") {
            inorder(rootid);
            cout << endl;
            preorder(rootid);
            cout << endl;

            // debug
            // for (int i = 0; i < counter; i++) {
            //     cout << "id = " << i << endl
            //          << "val = " << nodelist[i].val() << endl
            //          << "parent = " << nodelist[i].parent() << endl
            //          << "left = " << nodelist[i].left() << endl
            //          << "right = " << nodelist[i].right() << endl
            //          << endl;
            // }
        }
    }

    return 0;
}

Measured memory_bytes: 10516