#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <numeric>
#include <functional>
#include <iterator>
#include <limits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdint>
#include <cassert>

using namespace std;

#define FOR(i, start, end) for(int i = (start); i < (end); ++i)
#define REP(i, n) FOR(i, 0, n)

struct Node {
    int key;
    Node* parent;
    Node* left;
    Node* right;
    Node(int k) : key(k) {}
};

void bintree_insert(Node*& root, int key)
{
    Node* node = new Node(key);
    if(!root) {
        root = node;
        return;
    }

    Node* parent = root;
    for(;;) {
        Node*& child = key < parent->key ? parent->left : parent->right;
        if(!child) {
            node->parent = parent;
            child = node;
            break;
        }
        parent = child;
    }
}

bool bintree_find(const Node* root, int key)
{
    const Node* node = root;
    for(;;) {
        if(!node) return false;

        if(node->key == key) return true;

        node = key < node->key ? node->left : node->right;
    }
}

Node* inorder_next(const Node* node)
{
    Node* nxt = node->right; // ???????????¶??????????????? NULL ??§?????????
    while(nxt->left)
        nxt = nxt->left;
    return nxt;
}

void bintree_delete(Node*& root, int key)
{
    if(!root) return;
    if(root->key == key) {
        root = nullptr;
        return;
    }

    Node* node = root;
    for(;;) {
        if(!node) return;

        if(node->key == key) break;

        node = key < node->key ? node->left : node->right;
    }

    if(!node->left && !node->right) {
        if(node == node->parent->left)
            node->parent->left = nullptr;
        else
            node->parent->right = nullptr;
    }
    else if(node->left && node->right) {
        Node* nxt = inorder_next(node);

        node->key = nxt->key;
        if(nxt == nxt->parent->left)
            nxt->parent->left = nullptr;
        else
            nxt->parent->right = nullptr;
    }
    else {
        Node* child = node->left ? node->left : node->right;

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

void bintree_print_inorder(Node* node)
{
    if(!node) return;

    bintree_print_inorder(node->left);
    printf(" %d", node->key);
    bintree_print_inorder(node->right);
}

void bintree_print_preorder(Node* node)
{
    if(!node) return;

    printf(" %d", node->key);
    bintree_print_preorder(node->left);
    bintree_print_preorder(node->right);
}

void bintree_print(Node* root)
{
    bintree_print_inorder(root);
    puts("");

    bintree_print_preorder(root);
    puts("");
}

void interpret()
{
    int n;
    scanf("%d\n", &n);

    Node* root = nullptr;

    char buf[256];
    REP(_, n) {
        scanf("%s", buf);
        if(strcmp(buf, "insert") == 0) {
            //puts("insert");
            int key;
            scanf("%d", &key);
            bintree_insert(root, key);
        }
        else if(strcmp(buf, "find") == 0) {
            //puts("find");
            int key;
            scanf("%d", &key);
            bool found = bintree_find(root, key);
            puts(found ? "yes" : "no");
        }
        else if(strcmp(buf, "delete") == 0) {
            //puts("delete");
            int key;
            scanf("%d", &key);
            bintree_delete(root, key);
        }
        else if(strcmp(buf, "print") == 0) {
            //puts("print");
            bintree_print(root);
        }
    }
}

int main()
{
    interpret();

    return 0;
}

Measured memory_bytes: 21448