#include <iostream>
#include <string>

using namespace std;

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

    Node(int k) : key(k), parent(NULL), left(NULL), right(NULL) {}
};

void insert(Node** T, int k)
{
    Node* n = *T;
    Node* parent = NULL;
    Node* new_node = new Node(k);

    while (n != NULL) {
        parent = n;
        if (new_node->key < n->key)
            n = n->left;
        else
            n = n->right;
    }
    new_node->parent = parent;

    if (parent == NULL)
        *T = new_node;
    else if (new_node->key < parent->key)
        parent->left = new_node;
    else
        parent->right = new_node;
}

Node* find(Node* T, int k)
{
    Node* node = T;
    while (node != NULL) {
        if (node->key == k)
            return node;
        else if (node->key < k)
            node = node->right;
        else
            node = node->left;
    }
    return NULL;
}

Node* minimum(Node* node)
{
    if (node == NULL)
        return NULL;

    Node* min;
    Node* tmp = node;
    while (tmp != NULL) {
        min = tmp;
        tmp = tmp->left;
    }
    return min;
}

Node* successor(Node* node)
{
    if (node == NULL)
        return NULL;

    if (node->right != NULL)
        return minimum(node->right);

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

void delete_node(Node* T, Node* node)
{
    Node* parent = node->parent;

    if (node->left == NULL && node->right == NULL) {    // don't have child
        if (parent->left != NULL && parent->left == node)
            parent->left = NULL;
        else
            parent->right = NULL;
        delete node;
    } else if (node->left != NULL && node->right != NULL) { // have two children
        Node* next = successor(node);
        if (next == NULL)
            return;
        node->key = next->key;
        delete_node(T, next);
    } else {    // have only one child
        Node* child;
        if (node->left != NULL)
            child = node->left;
        else
            child = node->right;

        child->parent = parent;
        if (child->key < parent->key)
            parent->left = child;
        else
            parent->right = child;
        delete node;
    }
}

void inorder(Node* T)
{
    if (T == NULL)
        return;

    inorder(T->left);
    cout << ' ' << T->key;
    inorder(T->right);
}

void preorder(Node* T)
{
    if (T == NULL)
        return;

    cout << ' ' << T->key;
    preorder(T->left);
    preorder(T->right);
}

void print(Node* T)
{
    inorder(T);
    cout << '\n';
    preorder(T);
    cout << '\n';
}

void delete_tree(Node* T)
{
    if (T == NULL)
        return;

    delete_tree(T->left);
    delete_tree(T->right);
    delete T;
}

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

    Node* T = NULL;
    for (int i = 0; i < m; ++i) {
        string instruction;
        cin >> instruction;
        if (instruction == "insert") {
            int k;
            cin >> k;
            insert(&T, k);
        } else if (instruction == "find") {
            int k;
            cin >> k;
            Node* result = find(T, k);
            if (result != NULL)
                cout << "yes\n";
            else
                cout << "no\n";
        } else if (instruction == "delete") {
            int k;
            cin >> k;
            Node* node = find(T, k);
            if (node != NULL)
                delete_node(T, node);
        } else {
            print(T);
        }
    }

    delete_tree(T);
}



Measured memory_bytes: 21944