#include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

struct BinarySearchTree {
    BinarySearchTree *left = nullptr, *right = nullptr, *parent = nullptr;
    int value;
    BinarySearchTree(int value) : value(value) {}
};

BinarySearchTree* insert(BinarySearchTree *root, BinarySearchTree *z) {
    BinarySearchTree *y = nullptr;
    BinarySearchTree *x = root;

    while (x != nullptr) {
        y = x;
        if (z->value < x->value) {
            x = x->left;
        } else {
            x = x->right;
        }
    }

    if (y == nullptr) {
        return z;
    } else if (z->value < y->value) {
        y->left = z;
    } else {
        y->right = z;
    }

    z->parent = y;

    return root;
}

BinarySearchTree* find(BinarySearchTree *root, int value) {
    BinarySearchTree *x = root;
    while (x != nullptr) {
        if (value == x->value) {
            return x;
        } else if (value < x->value) {
            x = x->left;
        } else {
            x = x->right;
        }
    }
    return nullptr;
}

BinarySearchTree* delete_node(BinarySearchTree *z) {
    auto x = z;
    if (z == nullptr) { return x; }
    if (z->left == nullptr && z->right == nullptr) {
        if (z->parent != nullptr) {
            if (z->parent->left == z) {
                z->parent->left = nullptr;
            } else {
                z->parent->right = nullptr;
            }
        } else {
            x = nullptr;
        }
    } else if (z->right == nullptr) {
        if (z->parent != nullptr) {
            if (z->parent->left == z) {
                z->parent->left = z->left;
            } else {
                z->parent->right = z->left;
            }
        } else {
            x = z->left;
        }
        z->left->parent = z->parent;
    } else if (z->left == nullptr) {
        if (z->parent != nullptr) {
            if (z->parent->left == z) {
                z->parent->left = z->right;
            } else {
                z->parent->right = z->right;
            }
        } else {
            x = z->right;
        }
        z->right->parent = z->parent;
    } else {
        auto next = z->right;
        while (next->left != nullptr) {
            next = next->left;
        }
        z->value = next->value;
        delete_node(next);
        return z;
    }
    delete z;
    return x;
}

void preorder_print(BinarySearchTree *node) {
    if (node == nullptr) { return; }
    cout << " " << node->value;
    preorder_print(node->left);
    preorder_print(node->right);
}

void inorder_print(BinarySearchTree *node) {
    if (node == nullptr) { return; }
    inorder_print(node->left);
    cout << " " << node->value;
    inorder_print(node->right);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    string command;
    int value;
    BinarySearchTree *root = nullptr;

    FOR(i,0,n) {
        cin >> command;
        if (command == "insert") {
            cin >> value;
            auto z = new BinarySearchTree(value);
            root = insert(root, z);
        } else if (command == "find"){
            cin >> value;
            auto z = find(root, value);
            if (z != nullptr) {
                cout << "yes" << endl;
            } else {
                cout << "no" << endl;
            }
        } else if (command == "delete") {
            cin >> value;
            auto z = find(root, value);
            auto root_cand = delete_node(z);
            if (z == root) {
                root = root_cand;
            }
        } else {
            inorder_print(root);
            cout << endl;
            preorder_print(root);
            cout << endl;
        }
    }

    return 0;
}

Measured memory_bytes: 21896