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

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

Node *root, *NIL;

void insert(int key)
{
    Node *newnode = (Node *)malloc(sizeof(Node));
    newnode -> key = key;
    newnode -> parent = NIL;
    newnode -> left = NIL;
    newnode -> right = NIL;

    Node *x = root;
    Node *y = NIL;

    while (x != NIL)
    {
        y = x;

        if (x -> key >= newnode -> key)
        {
            x = x -> left;
        }
        else
        {
            x = x -> right;
        }
    }

    newnode -> parent = y;

    if (y == NIL)
    {
        root = newnode;
    }
    else
    {
        if (y -> key >= newnode -> key)
        {
            y -> left = newnode;
        }
        else
        {
            y -> right = newnode;
        }
    }
}

bool find(int key)
{
    Node *x = root;
    Node *y = NIL;

    while (x != NIL)
    {
        if (x -> key == key)
        {
            return true;
        }
        else if (x -> key > key)
        {
            x = x -> left;
        }
        else
        {
            x = x -> right;
        }
    }
    return false;
}

Node* getMinimum(Node *x)
{
    Node *minimum = x -> right;

    while (minimum -> left != NIL)
    {
        minimum = minimum -> left;
    }

    return minimum;
}

void deleteNode(int key)
{
    Node *x = root;
    Node *y = NIL;
    Node *z = NIL;

    while (x != NIL)
    {
        if (x -> key == key)
        {
            break;
        }
        else if (x -> key > key)
        {
            x = x -> left;
        }
        else
        {
            x = x -> right;
        }
    }

    if (x -> left == NIL || x -> right == NIL)
    {
        y = x;
    }
    else
    {
        y = getMinimum(x);
    }

    if (y -> left != NIL)
    {
        z = y -> left;
    }
    else
    {
        z = y -> right;
    }
    
    if (z != NIL)
    {
        z -> parent = y -> parent;
    }

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

    if (y != x)
    {
        x -> key = y -> key;
    }
    
    free(y);
}

void preParse(Node *u)
{
    if (u == NIL) return;
    cout << " " << u -> key;
    preParse(u -> left);
    preParse(u -> right); 
}

void inParse(Node *u)
{
    if (u == NIL) return;
    inParse(u -> left);
    cout << " " << u -> key;
    inParse(u -> right); 
}

int main()
{
    int m, key;
    string command;

    cin >> m;

    for (int i = 0; i != m; ++i)
    {
        cin >> command;

        if (command == "insert")
        {
            cin >> key;
            insert(key);
        }
        else if(command == "find")
        {
            cin >> key;
            if (find(key))
            {
                cout << "yes" << endl;
            }
            else
            {
                cout << "no" << endl;
            }
            
        }
        else if (command == "delete")
        {
            cin >> key;
            deleteNode(key);
        }
        else if (command == "print")
        {
            inParse(root);
            cout << endl;
            preParse(root);
            cout << endl;
        }
    }
    return 0;
}



Measured memory_bytes: 21804