#include <iostream>
#include <string>

using namespace std;

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

class BinarySearchTree {
private:
    Node *root = nullptr;
public:
    BinarySearchTree() {}

    BinarySearchTree(int key)
    {
        insert(key);
    }

    void insert(int key)
    {
        /* Nodeの準備 */
        Node *node = new Node;
        node->key = key;
        node->left = nullptr;
        node->right = nullptr;

        /* 根がNULL(木が空)の場合 */
        if (root == nullptr)
        {
            node->parent = nullptr;
            root = node;
            return;
        }

        Node *parent = root;
        while (true)
        {
            if (node->key <= parent->key)
            {
                if (parent->left == nullptr)
                {
                    parent->left = node;
                    node->parent = parent;
                    break;
                } else
                    parent = parent->left;
            } else
            {
                if (parent->right == nullptr)
                {
                    parent->right = node;
                    node->parent = parent;
                    break;
                } else
                    parent = parent->right;
            }
        }
    }

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

    Node *getMinimum(Node *root)
    {
        while(root->left != nullptr)
            root = root->left;
        return root;
    }

    Node *getSuccessor(Node *node)
    {
        if(node->right != nullptr)
            return getMinimum(node->right);

        Node *parent = node->parent;
        while(parent != nullptr && node == parent->right)
        {
            node = parent;
            parent = parent->parent;
        }
        return parent;
    }

    void delete_node(int key)
    {
        Node *node = find(key); // 削除したいノード
        if(node == nullptr) return;

        Node *del; // 実際に削除するノード
        if(node->left == nullptr || node->right == nullptr)
            del = node;
        else
            del = getSuccessor(node);

        /* delの子を決める */
        Node *child;
        if(del->left != nullptr)
            child = del->left;
        else
            child = del->right;

        if(child != nullptr)
            child->parent = del->parent;

        if(del->parent == nullptr)
            root = child;
        else if(del == del->parent->left)
            del->parent->left = child;
        else
            del->parent->right = child;

        if(del != node)
            node->key = del->key;

        delete del;
    }

    void inParse(Node *node)
    {
        if (node != nullptr)
        {
            inParse(node->left);
            cout << " " << node->key;
            inParse(node->right);
        }
    }

    void preParse(Node *node)
    {
        if (node != nullptr)
        {
            cout << " " << node->key;
            preParse(node->left);
            preParse(node->right);
        }
    }

    void print()
    {
        inParse(root);
        cout << endl;
        preParse(root);
        cout << endl;
    }
};

int main()
{
    int m;
    cin >> m;
    BinarySearchTree tree;
    string command;
    int key;
    for(int i = 0; i < m; i++)
    {
        cin >> command;
        if(command == "insert")
        {
            cin >> key;
            tree.insert(key);
        }
        else if(command == "find")
        {
            cin >> key;
            if(tree.find(key) != nullptr)
                cout << "yes" << endl;
            else
                cout << "no" << endl;
        }
        else if(command == "delete")
        {
            cin >> key;
            tree.delete_node(key);
        }
        else if(command == "print")
            tree.print();
    }
}



Measured memory_bytes: 21800