#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<double, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T>
void printv(const vector<T> &v, int idx)
{
    int sz = v.size();
    for (int i = 0; i < sz; i++)
    {
        if (i == idx)
        {
            cout << "[" << v[i] << "]"
                 << " \n"[i == sz - 1];
            continue;
        }
        cout << v[i] << " \n"[i == sz - 1];
    }
}

struct Node
{
    Node *right;
    Node *left;
    Node *par;
    ll key;

    Node(ll v) : key(v)
    {
        right = left = par = NULL;
    }
};

template <typename T>
struct BinarySearchTree
{
    Node *root;
    BinarySearchTree()
    {
        root = NULL;
    }
    void insert(T z)
    {
        insert(new Node(z));
    }
    void insert(Node *z)
    {
        Node *y = NULL, *x = root;
        while (x != NULL)
        {
            y = x;
            if (z->key < x->key)
            {
                x = x->left;
            }
            else
            {
                x = x->right;
            }
        }
        z->par = y;

        if (y == NULL)
        {
            root = z;
        }
        else if (z->key < y->key)
        {
            y->left = z;
        }
        else
        {
            y->right = z;
        }
    }
    void preorder(Node *cur)
    {
        if (cur == NULL)
            return;
        cout << " " << cur->key;
        preorder(cur->left);
        preorder(cur->right);
    }
    void inorder(Node *cur)
    {
        if (cur == NULL)
            return;
        inorder(cur->left);
        cout << " " << cur->key;
        inorder(cur->right);
    }
    void print()
    {
        inorder(root);
        cout << endl;
        preorder(root);
        cout << endl;
    }
    bool find(T v)
    {
        bool f = false;
        Node *x = root;
        while (x != NULL)
        {
            if (x->key == v)
            {
                f = true;
                break;
            }
            if (v < x->key)
            {
                x = x->left;
            }
            else
            {
                x = x->right;
            }
        }
        return f;
    }
    void Delete(T v)
    {
        Node *x = root;
        while (x != NULL && x->key != v)
        {
            if (v < x->key)
                x = x->left;
            else
                x = x->right;
        }
        Delete(x);
    }
    void Delete(Node *z)
    {
        if (z == NULL)
            return;
        if (z->left == NULL && z->right == NULL)
        {
            if (z->par != NULL)
            {
                if (z->par->left == z)
                    z->par->left = NULL;
                else
                    z->par->right = NULL;
            }
            delete z;
        }
        else if (z->left == NULL)
        {
            z->right->par = z->par;
            if (z->par != NULL)
            {
                if (z->par->left == z)
                    z->par->left = z->right;
                else
                    z->par->right = z->right;
            }
        }
        else if (z->right == NULL)
        {
            z->left->par = z->par;
            if (z->par != NULL)
            {
                if (z->par->left == z)
                    z->par->left = z->left;
                else
                    z->par->right = z->left;
            }
        }
        else
        {
            Node *x = z->right, *y = z;
            while (x != NULL)
            {
                y = x;
                x = x->left;
            }
            z->key = y->key;
            Delete(y);
        }
    }
};

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

    int m;
    cin >> m;
    BinarySearchTree<ll> bst = BinarySearchTree<ll>();
    for (int i = 0; i < m; i++)
    {
        string s;
        cin >> s;
        if (s == "insert")
        {
            ll key;
            cin >> key;
            bst.insert(key);
        }
        else if (s == "find")
        {
            ll key;
            cin >> key;
            cout << (bst.find(key) ? "yes" : "no") << endl;
        }
        else if (s == "delete")
        {
            ll key;
            cin >> key;
            bst.Delete(key);
        }
        else
        {
            bst.print();
        }
    }
}


Measured memory_bytes: 21900