#include <bits/stdc++.h>
using namespace std;

class TreeNode {
public:
    TreeNode *left;
    TreeNode *right;
    int val;
    TreeNode(int val) : left(nullptr), right(nullptr), val(val) {}
};

TreeNode* insert(TreeNode* root, int val)
{
    if (!root) return new TreeNode(val);
    TreeNode* curr = root;
    TreeNode* prev = nullptr;
    while (curr) {
        prev = curr;
        if (val < curr->val) {
            curr = curr->left;
        } else {
            curr = curr->right;
        }
    }
    if (val < prev->val)
        prev->left = new TreeNode(val);
    else
        prev->right = new  TreeNode(val);
    return root;
}

TreeNode* del(TreeNode* root, int val)
{
    if (!root) return root;
    TreeNode* prev = nullptr;
    TreeNode* curr = root;
    while (curr) {
        if (val < curr->val) {
            prev = curr;
            curr = curr->left;
        } else if (val > curr->val) {
            prev = curr;
            curr = curr->right;
        } else {
            if (!curr->right) {
                if (prev->left == curr) {
                    prev->left = curr->left;
                } else {
                    prev->right = curr->left;
                }
                delete curr;
            } else if (!curr->left) {
                if (prev->left == curr) {
                    prev->left = curr->right;
                } else {
                    prev->right = curr->right;
                }
                delete curr;
            } else {
                TreeNode* q = curr;
                TreeNode* p = curr->right;
                while (p->left) {
                    q = p;
                    p = p->left;
                }
                curr->val = p->val;
                if (q->left == p) {
                    q->left = p->right;
                } else {
                    q->right = p->right;
                }
                delete p;
            }
            break;
        }
    }
    return root;
}

bool find(TreeNode* root, int val)
{
    if (!root) return false;

    TreeNode* curr = root;
    while (curr) {
        if (val < curr->val) {
            curr = curr->left;
        } else if (val > curr->val) {
            curr = curr->right;
        } else {
            return true;
        }
    }
    return false;
}

void inorder(TreeNode* root)
{
    if (!root) return;
    inorder(root->left);
    cout << " " << root->val;
    inorder(root->right);
}

void preorder(TreeNode* root)
{
    if (!root) return;
    cout << " " << root->val;
    preorder(root->left);
    preorder(root->right);
}

int main()
{
    int n;
    cin >> n;
    string s;
    int val;
    TreeNode* tree = nullptr;
    for (int i = 0; i < n; i++) {
        cin >> s;
        if (s == "insert") {
            cin >> val;
            tree = insert(tree, val);
        } else if (s == "find") {
            cin >> val;
            if (find(tree, val)) {
                cout << "yes" << endl;
            } else {
                cout << "no" << endl;
            }
        } else if (s == "delete") {
            cin >> val;
            tree = del(tree, val);
        } else if (s == "print") {
            inorder(tree);
            cout << endl;
            preorder(tree);
            cout << endl;
        }
    }
}


Measured memory_bytes: 15712