#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

struct Node
{
    int key;
    Node *parent, *right, *left;
};

Node *root; //初めはnullptr

void Insert(int key)
{
    Node *y = nullptr;
    Node *x = root;
    Node *z = new Node;
    z->key = key;

    //不要
    z->left = nullptr;
    z->right = nullptr;

    while (x != nullptr)
    {
        y = x;
        if (z->key < x->key)
        {
            x = x->left;
        }
        else
        {
            x = x->right;
        }
    }

    z->parent = y;
    if (z->parent == nullptr)
    {
        root = z;
    }
    else if (z->key < y->key)
    {
        y->left = z;
    }
    else
    {
        y->right = z;
    }
}

Node *Find(int key)
{
    Node *x = root;
    while (x != nullptr && key != x->key)
    {
        if (key < x->key)
        {
            x = x->left;
        }
        else
        {
            x = x->right;
        }
    }

    return x;
}

void Delete(int key)
{
    Node *x;
    x = Find(key);

    if (x == nullptr)
    {
        return;
    }

    if (x->right == nullptr && x->left == nullptr)
    {
        if (x->parent->key < key)
        {
            x->parent->right = nullptr;
        }
        else
        {
            x->parent->left = nullptr;
        }
        delete x;
    }
    else if (x->right != nullptr && x->left == nullptr)
    {
        if (x == root)
        {
            root = x->right;
            x->right->parent = nullptr;
            delete x;
        }
        else if (x->parent->key > key)
        {
            x->parent->left = x->right;
            x->right->parent = x->parent;
        }
        else
        {
            x->parent->right = x->right;
            x->right->parent = x->parent;
        }
        delete x;
    }
    else if (x->left != nullptr && x->right == nullptr)
    {
        if (x == root)
        {
            root = x->left;
            x->left->parent = nullptr;
            delete x;
        }
        if (x->parent->key > key)
        {
            x->parent->left = x->left;
            x->left->parent = x->parent;
        }
        else
        {
            x->parent->right = x->left;
            x->left->parent = x->parent;
        }

        delete x;
    }
    else
    {
        //子を二つ持っているとき
        Node *y = x->right;
        while (y->left != nullptr)
        {
            y = y->left;
        }

        if (x->right == y)
        {
            x->key = y->key;
            if (y->right != nullptr)
            {
                x->right = y->right;
                y->right->parent = x;
            }
            else
            {
                x->right = nullptr;
            }

            delete y;
        }
        else
        {
            x->key = y->key;
            if (y->right != nullptr)
            {
                y->right->parent = y->parent;
                y->parent->left = y->right;
            }
            else
            {
                y->parent->left = nullptr;
            }
            delete y;
        }

        /*if (y != x->right)
        {
            if (y->right != nullptr)
            {
                y->right->parent = y->parent;
            }
            y->parent->left = y->right;

            y->right = x->right;
            x->right->parent = y;
        }
        else
        {
        }
        y->left = x->left;
        x->left->parent = y;
        y->parent = x->parent;
        if (x->parent != nullptr)
        {
            if (x->parent->key < key)
            {
                x->parent->right = y;
            }
            else
            {
                x->parent->left = y;
            }
        }
        delete x;*/

        //cout << y->key << endl;
    }
}
void Inorder(Node *u)
{
    if (u == nullptr)
    {
        return;
    }
    Inorder(u->left);
    cout << ' ' << u->key;
    Inorder(u->right);
}

void Preorder(Node *u)
{
    if (u == nullptr)
    {
        return;
    }
    cout << ' ' << u->key;
    Preorder(u->left);
    Preorder(u->right);
}

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

    int num;
    string tmp;
    int key;

    cin >> num;
    for (int i = 0; i < num; i++)
    {
        cin >> tmp;
        if (tmp == "insert")
        {
            cin >> key;
            Insert(key);
        }
        else if (tmp == "find")
        {
            cin >> key;
            if (Find(key) != nullptr)
            {
                cout << "yes" << endl;
            }
            else
            {
                cout << "no" << endl;
            }
        }
        else if (tmp == "delete")
        {
            cin >> key;
            Delete(key);
        }
        else if (tmp == "print")
        {
            Inorder(root);
            cout << endl;
            Preorder(root);
            cout << endl;
        }
    }

    return 0;
}


Measured memory_bytes: 21780