#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
template<class Type> class BinaryTree
{
    struct Node
    {
        Type val;
        Node *left, *right;
    };
    public:
        Node *root = NULL;
        Node *insert(Node *p, Type x)
        {
            if(p == NULL)
            {
                Node *q = new Node;
                q->val = x;
                q->left = q->right = NULL;
                if(root == NULL) root = q;
                return q;
            }
            else
            {
                if(x < p->val) p->left = insert(p->left, x);
                else p->right = insert(p->right, x);
                return p;
            }
        }
        bool find(Node *p, Type x)
        {
            if(p == NULL) return false;
            else if(x == p->val) return true;
            else if(x < p->val) return find(p->left, x);
            else return find(p->right, x);
        }
        Node *remove(Node *p, Type x)
        {
            if(p == NULL) return NULL;
            else if(x < p->val) p->left = remove(p->left, x);
            else if(x > p->val) p->right = remove(p->right, x);
            //以下、削除したいノードが見つかったときの操作
            else if(p->left == NULL && p->right == NULL)
            {
                delete p;
                return NULL;
            }
            else if(p->right == NULL)
            {
                Node *q = p->left;
                delete p;
                return q;
            }
            else if(p->left == NULL) 
            {
                Node *q = p->right;
                delete p;
                return q;
            }
            else if(p->right->left == NULL)
            {
                Node *q = p->right;
                q->left = p->left;
                delete p;
                return q;
            }
            else
            {
                Node *q;
                for(q = p->right; q->left->left != NULL; q = q->left);
                Node *r = q->left;
                q->left = r->right;
                r->left = p->left, r->right = p->right;
                delete p;
                return r;
            }
            return p;
        }
        void print()
        {
            inorder(root); cout << endl;
            preorder(root); cout << endl;
        }
        void inorder(Node *p)
        {
            if(p->left != NULL) inorder(p->left);
            cout << " " << p->val;
            if(p->right != NULL) inorder(p->right);
        }
        void preorder(Node *p)
        {
            cout << " " << p->val;
            if(p->left != NULL) preorder(p->left);
            if(p->right != NULL) preorder(p->right);
        }
};
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    BinaryTree<int> T;
    for(int i = 0; i < n; i++)
    {
        string s; cin >> s;
        if(s == "insert") 
        {
            int n; cin >> n;
            T.insert(T.root, n);
        }
        if(s == "find")
        {
            int n; cin >> n;
            cout << (T.find(T.root, n) ? "yes" : "no") << endl;
        }
        if(s == "delete")
        {
            int n; cin >> n;
            T.remove(T.root, n);
        }
        if(s == "print") T.print();
    }
    return 0;
}




Measured memory_bytes: 15624