#include <iostream>
using namespace std;

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

Node *root;
Node *nil;

void InsertNode(int key)
{
    Node *x = root;
    Node *y = nil;
    Node *z = (Node *)malloc(sizeof(Node));
    z->key = key;
    z->parent = nil;
    z->left = nil;
    z->right = nil;

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

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

    return;
}

Node *FindNode(int key, Node *x)
{
    while (x != nil)
    {
        if (x->key == key)
        {
            return x;
        }
        else if (key < x->key)
        {
            x = x->left;
        }
        else
        {
            x = x->right;
        }
    }

    return nil;
}

Node *FindByInOrder(Node *x)
{
    if (x->left == nil)
    {
        return x;
    }
    else
    {
        return FindByInOrder(x->left);
    }
}

void DeleteNode(Node *x)
{
    if (x->left == nil && x->right == nil)
    {
        if (x->parent == nil)
        {
            x = nil;
        }
        else if (x->key < x->parent->key)
        {
            x->parent->left = nil;
            x = nil;
        }
        else
        {
            x->parent->right = nil;
            x = nil;
        }
    }
    else if (x->left != nil && x->right != nil)
    {
        Node *y = FindByInOrder(x->right);
        x->key = y->key;
        DeleteNode(y);
    }
    else
    {
        if (x->left != nil)
        {
            if (x->key < x->parent->key)
            {
                x->left->parent = x->parent;
                x->parent->left = x->left;
            }
            else
            {
                x->left->parent = x->parent;
                x->parent->right = x->left;
            }
        }
        else
        {
            if (x->key < x->parent->key)
            {
                x->right->parent = x->parent;
                x->parent->left = x->right;
            }
            else
            {
                x->right->parent = x->parent;
                x->parent->right = x->right;
            }
        }
        x = nil;
    }
}

void PrintInOrder(Node *x)
{
    if (x == nil)
    {
        return;
    }

    PrintInOrder(x->left);
    cout << " " << x->key;
    PrintInOrder(x->right);
}

void PrintPreOrder(Node *x)
{
    if (x == nil)
    {
        return;
    }

    cout << " " << x->key;
    PrintPreOrder(x->left);
    PrintPreOrder(x->right);
}

int main()
{
    int n;
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        string s;
        cin >> s;
        if (s == "insert")
        {
            int key;
            cin >> key;
            InsertNode(key);
        }
        else if (s == "find")
        {
            int key;
            cin >> key;
            if (FindNode(key, root) != nil)
            {
                cout << "yes" << endl;
            }
            else
            {
                cout << "no" << endl;
            }
        }
        else if (s == "delete")
        {
            int key;
            cin >> key;
            Node *delete_node = FindNode(key, root);
            DeleteNode(delete_node);
        }
        else
        {
            PrintInOrder(root);
            cout << endl;
            PrintPreOrder(root);
            cout << endl;
        }
    }

    return 0;
}


Measured memory_bytes: 21896