#include <iostream>
#include <vector>
#include <string>
#include <memory>
#include <utility>
using namespace std;

struct Node {
    int key;
    shared_ptr<Node> left, right, parent;
    Node(): left(nullptr), right(nullptr), parent(nullptr) {

    }
    Node(const int value):key(value) {
        
    }
};

void insert(shared_ptr<Node>& node, int key, shared_ptr<Node>& parent) {
    if(node == nullptr) {
        node = make_shared<Node>(key);
        node->parent = parent;
    } else {
        if(key < node->key) {
            insert(node->left, key, node);
        } else {
            insert(node->right, key, node);
        }
    }
}

void printInorder(shared_ptr<Node>& node) {
    if(node == nullptr) {
        return;
    }
    printInorder(node->left);
    cout << " " << node->key;
    printInorder(node->right);
}

void printPreorder(shared_ptr<Node>& node){
    if (node == nullptr) {
        return;
    }
    cout << " " << node->key;
    printPreorder(node->left);
    printPreorder(node->right);
}

shared_ptr<Node> find(shared_ptr<Node>& node, int key) {
    if(node == nullptr) {
        return nullptr;
    }
    if(node->key == key) {
        return node;
    }
    if(key < node->key) {
       return find(node->left, key);
    } else {
       return find(node->right, key);
    }
}

shared_ptr<Node> findSuccessor(shared_ptr<Node>& node) {
    if(node->left == nullptr) {
        return node;
    }
    return findSuccessor(node->left);
}

void deleteNode(shared_ptr<Node>& node, shared_ptr<Node>& root) {
    // 削除対象が子を持たない、もしくは一つの子を持つときは
    if(node->left == nullptr || node->right == nullptr) {
        // 引数のnodeを消す
        // 子供から設定
        shared_ptr<Node> child;
        if(node->left != nullptr) {
            child = node->left;
        } else {
            child = node->right;
        }
        if(child != nullptr) {
            child->parent = node->parent;
        }

        // 削除対象が根なら子をrootにする
        if(node == root) {
            root = child;
        } else {
            // 削除対象は親の左の子か右の子なのか
            if(node->parent->left == node) {
                node->parent->left = child;
            } else {
                node->parent->right = child;
            }
        }
    } else {
        // 次節点
        shared_ptr<Node> next = findSuccessor(node->right);
        // 次節点のキーと削除したいNodeのキーを入れ替える
        node->key = next->key;
        shared_ptr<Node> child;
        if(next->left != nullptr) {
            child = next->left;
        } else {
            child = next->right;
        }
        if(child != nullptr) {
            child->parent = next->parent;
        }

        if(next->parent->left == next) {
            next->parent->left = child;
        } else {
            next->parent->right = child;
        }
    }
    /*
    if(node->left == nullptr && node->right == nullptr) {
        if(node->parent->left == node) {
            node->parent->left = nullptr;
        } else {
            node->parent->right = nullptr;
        }
    } else if(node->left != nullptr && node->right != nullptr) {
        auto next = findSuccessor(node->right);
        // 削除点のキーを次節点のキーに変える
        node->key = next->key;
        // 次節点に子が存在するならば、次節点の親と子をつなげる
        if(next->right != nullptr) {
            next->parent->left = next->right;
            next->right->parent = next->parent;
        } else {
            next->parent->left = nullptr;
        }
    } else {
        // 右をすげ替える
        if(node->left == nullptr) {
            if(node->parent->left == node) {
                node->parent->left = node->right;
            } else {
                node->parent->right = node->right;
            }
            node->right->parent = node->parent;
        } else {
            if(node->parent->left == node) {
                node->parent->left = node->left;
            } else {
                node->parent->right = node->left;
            }
            node->left->parent = node->parent;
        }
    }
    */
}

int main() {
    int num;
    cin >> num;
    shared_ptr<Node> t(nullptr);
    shared_ptr<Node> p(nullptr);
    for(int i=0; i<num; i++) {
        string command;
        cin >> command;
        if(command == "insert") {
            int key;
            cin >> key;
            insert(t, key, p);
        } else if(command == "print") {
            printInorder(t);
            cout << endl;
            printPreorder(t);
            cout << endl;
        } else if(command == "find"){
            int key;
            cin >> key;
            auto result = find(t, key);
            if(result) {
                cout << "yes" << endl;
            } else {
                cout << "no" << endl;
            }
        } else if(command == "delete") {
            int key;
            cin >> key;
            auto result = find(t,key);
            deleteNode(result, t);
        }
    }
}


Measured memory_bytes: 34432