#include <bits/stdc++.h>
using namespace std;

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

class BinSearchTree
{
private:
    Node *root;

    Node *allocNode()
    {
        Node *node = (Node *)malloc(sizeof(Node));
        return node;
    };

    void setNode(Node *node, int key, Node *parent, Node *left, Node *right)
    {
        node->key = key;
        node->parent = parent;
        node->left = left;
        node->right = right;
    };

    Node *addNode(Node *node, int key)
    {
        if (node == NULL)
        {
            node = allocNode();
            setNode(node, key, NULL, NULL, NULL);
        }
        else if (key == node->key)
        {
            ;
        }
        else if (key < node->key)
        {
            node->left = addNode(node->left, key);
            node->left->parent = node;
        }
        else
        {
            node->right = addNode(node->right, key);
            node->right->parent = node;
        }

        return node;
    };

    void preorder(Node *node)
    {
        if (node == NULL)
            return;
        printf(" %d", node->key);
        preorder(node->left);
        preorder(node->right);
    };

    void inorder(Node *node)
    {
        if (node == NULL)
            return;
        inorder(node->left);
        printf(" %d", node->key);
        inorder(node->right);
    };

    bool findNode(int key, Node *node)
    {
        if (node == NULL)
            return false;
        if (key == node->key)
            return true;
        if (key < node->key)
            return findNode(key, node->left);
        return findNode(key, node->right);
    }

public:
    BinSearchTree()
    {
        root = NULL;
    };

    void insert(int key)
    {
        root = addNode(root, key);
    };

    void printPreorder()
    {
        preorder(root);
    };

    void printInorder()
    {
        inorder(root);
    };

    bool find(int key)
    {
        if (findNode(key, root))
        {
            cout << "yes" << endl;
            return true;
        }

        cout << "no" << endl;
        return false;
    };

    void deleteNode(int key)
    {
        Node *tmp;
        Node **right;
        Node **node = &root;

        while (*node != NULL)
        {
            if ((*node)->key == key)
            {
                tmp = *node;
                if (tmp->right == NULL && tmp->left == NULL)
                    *node = NULL;
                else if (tmp->left == NULL)
                    *node = tmp->right;
                else if (tmp->right == NULL)
                    *node = tmp->left;
                else
                {
                    right = &(*node)->right;
                    while ((*right)->left != NULL)
                        right = &(*right)->left;
                    *node = *right;
                    *right = (*right)->right;
                    (*node)->left = tmp->left;
                    (*node)->right = tmp->right;
                }
                free(tmp);
                return;
            }
            else if ((*node)->key > key)
                node = &(*node)->left;
            else
                node = &(*node)->right;
        }
    }
};

int main()
{
    int n;
    BinSearchTree tree;
    cin >> n;
    string s;
    while (n--)
    {
        cin >> s;
        if (s == "insert")
        {
            int a;
            cin >> a;
            tree.insert(a);
        }
        else if (s == "find")
        {
            int a;
            cin >> a;
            tree.find(a);
        }
        else if (s == "delete")
        {
            int a;
            cin >> a;
            tree.deleteNode(a);
        }
        else if (s == "print")
        {
            tree.printInorder();
            cout << endl;
            tree.printPreorder();
            cout << endl;
        }
    }
}


Measured memory_bytes: 22000