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

class Tree 
{
public:
    int value;
    Tree *left, *right;
    Tree(int value, Tree *left, Tree *right)
    {
        this->value = value;
        this->left = left;
        this->right = right;
    }
};

Tree *t_insert(Tree *root, int k)
{
    if (root == NULL)
        return new Tree(k, NULL, NULL);
    Tree *current = root;
    while (true)
    {
        if (k <= current->value)
        {
            if (current->left == NULL)
            {
                current->left = new Tree(k, NULL, NULL);
                return root;
            }
            else
                current = current->left;
        }
        else if (k > current->value)
        {
            if (current->right == NULL)
            {
                current->right = new Tree(k, NULL, NULL);
                return root;
            }
            else
                current = current->right;
        }
    }
}

void t_find(Tree *root, int k)
{
   if (root == NULL)
   {
       cout << "no" << endl;
       return;
   } 
   Tree *current = root;
   while (true)
   {
       if (current->value == k)
       {
           cout << "yes" << endl;
           return;
       }
       else if (k < current->value)
       {
           if (current->left == NULL)
           {
               cout << "no" << endl;
               return;
           }
           else current = current->left;
       }
       else
       {
           if (current->right == NULL)
           {
               cout << "no" << endl;
               return;
           }
           else current = current->right;
       }
   }
}

Tree *t_delete(Tree *root, int k)
{
    if (root == NULL) return root;
    Tree *current = root;
    Tree *parent = NULL;
    while (true)
    {
        if (k == current->value)
        {
            if (current->left == NULL && current->right == NULL)
            {
                if (parent == NULL)
                {
                    delete current;
                    return root;
                }
                else if (parent->left == current)
                {
                    parent->left = NULL;
                    delete current;
                    return root;
                }
                else
                {
                    parent->right = NULL;
                    delete current;
                    return root;
                }
            }
            else if (current->left == NULL)
            {
                if (parent == NULL)
                {
                    root = current->right;
                    delete current;
                    return root;
                }
                else if (parent->left == current)
                {
                    parent->left = current->right;
                    delete current;
                    return root;
                }
                else
                {
                    parent->right = current->right;
                    delete current;
                    return root;
                }
            }
            else if (current->right == NULL)
            {
                if (parent == NULL)
                {
                    root = current->left;
                    delete current;
                    return root;
                }
                else if (parent->left == current)
                {
                    parent->left = current->left;
                    delete current;
                    return root;
                }
                else
                {
                    parent->right = current->left;
                    delete current;
                    return root;
                }
            }
            else
            {
                Tree *successor = current->right;
                Tree *successor_parent = current;
                while (successor->left != NULL)
                {
                    successor_parent = successor;
                    successor = successor->left;
                }
                current->value = successor->value;
                if (successor_parent->left == successor)
                {
                    successor_parent->left = successor->right;
                    delete successor;
                    return root;
                }
                else
                {
                    successor_parent->right = successor->right;
                    delete successor;
                    return root;
                }
            }
        }
        else if (k < current->value)
        {
            if (current->left == NULL) return root;
            parent = current;
            current = current->left;
        }
        else
        {
            if (current->right == NULL) return root;
            parent = current;
            current = current->right;
        }
    }
}

void t_inorder(Tree *current)
{
    if (current == NULL)
        return;
    t_inorder(current->left);
    cout << ' ' << current->value;
    t_inorder(current->right);
}

void t_preorder(Tree *current)
{
    if (current == NULL)
        return;
    cout << ' ' << current->value;
    t_preorder(current->left);
    t_preorder(current->right);
}

int main(int argc, char const *argv[])
{
    Tree *root = NULL;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        string operation;
        int k;
        cin >> operation;
        if (operation == "insert")
        {
            cin >> k;
            root = t_insert(root, k);
        }
        else if (operation == "find")
        {
            cin >> k;
            t_find(root, k);
        }
        else if (operation == "delete")
        {
            cin >> k;
            root = t_delete(root, k);
        }
        else
        {
            t_inorder(root);
            cout << endl;
            t_preorder(root);
            cout << endl;
        }
    }
    return 0;
}



Measured memory_bytes: 15652