#include <iostream>
#include <string>

using namespace std;

class Node {
public:
    Node(int key, Node* parent = nullptr) : key_(key), parent_(parent), leftChild_(nullptr), rightChild_(nullptr) {}
    void insert(int key) {
        Node* y = nullptr;  // x の親
        Node* x = this;
        while (x != nullptr) {
            y = x;  // 親を設定
            if (key < x->key_) {
                x = x->leftChild_;  // 左の子へ移動
            }
            else {
                x = x->rightChild_; // 右の子へ移動
            }
        }
        Node* z = new Node(key, y);
        if (key < y->key_) {
            y->leftChild_ = z;
        }
        else {
            y->rightChild_ = z;
        }
    }
    bool find(int key) {
        Node* x = this;
        while (x != nullptr) {
            if (key == x->key_) {
                return true;
            }
            else if (key < x->key_) {
                x = x->leftChild_;  // 左の子へ移動
            }
            else {
                x = x->rightChild_; // 右の子へ移動
            }
        }
        return false;
    }
    void erase(int key) {
        Node* x = this;
        while (x != nullptr) {
            if (key == x->key_) {
                x->erase();
                return;
            }
            else if (key < x->key_) {
                x = x->leftChild_;  // 左の子へ移動
            }
            else {
                x = x->rightChild_; // 右の子へ移動
            }
        }
    }
    Node* findNext() {
        Node* x = this;
        while (x->leftChild_ != nullptr) {
            x = x->leftChild_;
        }
        return x;
    }
    void erase() {
        if ((leftChild_ != nullptr) && (rightChild_ != nullptr)) { // 子を２つ持つ場合
            Node* next = rightChild_->findNext();
            key_ = next->key_;
            next->eraseNoChildOrOneChild();
        }
        else {
            eraseNoChildOrOneChild();
        }
    }

    void eraseNoChildOrOneChild() {
        if ((leftChild_ == nullptr) && (rightChild_ == nullptr)) {  // 子を持たない場合→自身を削除
            if (parent_->leftChild_ == this) {
                parent_->leftChild_ = nullptr;
            }
            else {
                parent_->rightChild_ = nullptr;
            }
            delete this;
        }
        else {  // ちょうど１つの子を持つ場合
            Node* child = leftChild_ != nullptr ? leftChild_ : rightChild_;
            if (parent_->leftChild_ == this) {
                parent_->leftChild_ = child;
            }
            else {
                parent_->rightChild_ = child;
            }
            child->parent_ = parent_;
            delete this;
        }
    }
    void printInorder() {
        if (leftChild_ != nullptr) {
            leftChild_->printInorder();
        }
        cout << " " << key_;
        if (rightChild_ != nullptr) {
            rightChild_->printInorder();
        }
    }
    void printPreorder() {
        cout << " " << key_;
        if (leftChild_ != nullptr) {
            leftChild_->printPreorder();
        }
        if (rightChild_ != nullptr) {
            rightChild_->printPreorder();
        }
    }
private:
    int key_;
    Node* parent_;
    Node* leftChild_;
    Node* rightChild_;
};

class Tree {
public:
    Tree() : root_(nullptr) {}
    void insert(int key) {
        if (root_ == nullptr) {
            root_ = new Node(key);
        }
        else {
            root_->insert(key);
        }
    }
    bool find(int key) {
        if (root_ == nullptr) {
            return false;
        }
        return root_->find(key);
    }
    void erase(int key) {
        if (root_ == nullptr) {
            return;
        }
        root_->erase(key);
    }
    void print() {
        if (root_ == nullptr) {
            return;
        }
        root_->printInorder();
        cout << endl;
        root_->printPreorder();
        cout << endl;
    }
private:
    Node* root_;
};

int main()
{
    int n;
    cin >> n;
    string operation;
    Tree tree;
    for (int i = 0; i < n; ++i) {
        cin >> operation;
        if (operation == "insert") {
            int x;
            cin >> x;
            tree.insert(x);
        }
        else if (operation == "find") {
            int x;
            cin >> x;
            if (tree.find(x)) {
                cout << "yes" << endl;
            }
            else {
                cout << "no" << endl;
            }
        }
        else if (operation == "delete") {
            int x;
            cin >> x;
            tree.erase(x);
        }
        else {
            tree.print();
        }

    }
}



Measured memory_bytes: 21860