#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

class BinarySearchTree
{
public:
    BinarySearchTree()
    : root((Node*)nullptr)
    {}
    ~BinarySearchTree() {}

    struct Node
    {
        Node() {}
        Node(int u)
        : key(u), p((Node*)nullptr), l((Node*)nullptr), r((Node*)nullptr)
        {}
        Node(const Node& node)
        : key(node.key), p(node.p), l(node.l), r(node.r)
        {}
        ~Node() {} 
        int key;
        Node *p, *l, *r;
    };

    void insert(int u)
    {
        Node* y = nullptr;
        Node* x = root;
        Node* z = new Node(u);
        while (x != nullptr)
        {
            y = x;
            if (z->key < x->key)
            {
                x = x->l;
            }
            else
            {
                x = x->r;
            }
        }
        z->p = y;

        if (y == nullptr)
        {
            root = z;
        }
        else if (z->key < y->key)
        {
            y->l =z;
        }
        else
        {
            y->r = z;
        }
    }

    Node* find(int key)
    {
        Node* x = root;
        while (x)
        {
            if (x->key == key)
            {
                return x;
            }
            else if (key < x->key)
            {
                x = x->l;
            }
            else
            {
                x = x->r;
            }
        }
        return (Node*)nullptr;
    }

    void del(int key)
    {
        Node* x = find(key);
        if (!x->l && !x->r)
        {
            if (key < x->p->key)
            {
                x->p->l = nullptr;
            }
            else
            {
                x->p->r = nullptr;
            }
            delete x;
        }
        else if (x->l && !x->r)
        {
            if (key < x->p->key)
            {
                x->l->p = x->p;
                x->p->l = x->l;
            }
            else
            {
                x->l->p = x->p;
                x->p->r = x->l;
            }
            delete x;
        }
        else if (!x->l && x->r)
        {
            if (key < x->p->key)
            {
                x->r->p = x->p;
                x->p->l = x->r;
            }
            else
            {
                x->r->p = x->p;
                x->p->r = x->r;
            }
            delete x;
        }
        else
        {
            Node* y = x->r;
            while (y->l) y = y->l;
            x->key = y->key;
            if (y->p == x) y->p->r = nullptr;
            else y->p->l = nullptr;
            delete y;
        }
    }

    void preorder(Node* u)
    {
        Node* l = u->l;
        Node* r = u->r;
        cout << " " << u->key;
        if (l) preorder(l);
        if (r) preorder(r);
    }

    void inorder(Node* u)
    {
        Node* l = u->l;
        Node* r = u->r;
        if (l) inorder(l);
        cout << " " << u->key;
        if (r) inorder(r);
    }

    Node* root;
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    BinarySearchTree bst;
    char cmd[10];
    int u;
    rep (i, n)
    {
        cin >> cmd;
        // cout << i << ": cmd, u = " << cmd << ", " << u << "\n";
        if (cmd[0] == 'i')
        {
            cin >> u;
            bst.insert(u);
        }
        else if (cmd[0] == 'f')
        {
            cin >> u;
            if (bst.find(u)) cout << "yes\n";
            else cout << "no\n";
        }
        else if (cmd[0] == 'd')
        {
            cin >> u;
            bst.del(u);
        }
        else if (cmd[0] == 'p')
        {
            bst.inorder(bst.root);
            cout << "\n";
            bst.preorder(bst.root);
            cout << "\n";
        }
    }

    return 0;
}




Measured memory_bytes: 21896