#include<iostream>
#include<string>
using namespace std;

struct Node{
    int val;
    Node* parent;
    Node* left;
    Node* right;
};
Node* nil;

class Tree{
    Node* root;
    void preorder(Node* p){
        if (p == nil) return;
        cout << " " << p->val;
        preorder(p->left);
        preorder(p->right);
    }
    void inorder(Node* p){
        if (p == nil) return;
        inorder(p->left);
        cout << " " << p->val;
        inorder(p->right);
    }
    Node* next_node(Node* z){
        if (z->left == nil) return z;
        return next_node(z->left);
    }
    void delete_node(Node* z){
        if (z->left == nil && z->right == nil) {
            if (z->parent->left == z) {
                z->parent->left = nil;
            } else {
                z->parent->right = nil;
            }
            return;
        }
        if (z->left == nil) {
            if (z->parent->left == z) {
                z->parent->left = z->right;
            } else {
                z->parent->right = z->right;
            }
            z->right->parent = z->parent;
            return;
        }
        if (z->right == nil) {
            if (z->parent->left == z) {
                z->parent->left = z->left;
            } else {
                z->parent->right = z->left;
            }
            z->left->parent = z->parent;
            return;
        }
        Node* next = next_node(z->right);
        z->val = next->val;
        delete_node(next);
    }
public:
    Tree(){
        root = nil;
    }
    void insert(int k){
        Node* n = new Node;
        n->val   = k;
        n->left  = nil;
        n->right = nil;
        if (root == nil) {
            root = n;
            n->parent = nil;
            return;
        }
        Node* pos = root;
        Node* pos_p;
        while (pos != nil) {
            pos_p = pos;
            if (k < pos->val) {
                pos = pos->left;
                continue;
            }
            pos = pos->right;
        }
        n->parent = pos_p;
        if (k < pos_p->val) {
            pos_p->left = n;
        } else {
            pos_p->right = n;
        }
    }
    void print(){
        inorder(root);
        cout << endl;
        preorder(root);
        cout << endl;
    }
    bool find(int k){
        Node* p = root;
        while (p != nil) {
            if (k == p->val) return true;
            if (k < p->val) {
                p = p->left;
            } else {
                p = p->right;
            }
        }
        return false;
    }
    void del(int k){
        Node* z = root;
        while (z != nil) {
            if (k == z->val) break;
            if (k < z->val) {
                z = z->left;
            } else {
                z = z->right;
            }
        }
        delete_node(z);
    }
};

int main(){
    int n;
    cin >> n;
    Tree T;
    for (int i = 0; i < n; i++) {
        string str;
        cin >> str;
        if (str == "print") {
            T.print();
            continue;
        }
        int k;
        cin >> k;
        if (str == "insert") {
            T.insert(k);
            continue;
        }
        if (str == "find") {
            if (T.find(k)) {
                cout << "yes" << endl;
            } else {
                cout << "no" << endl;
            }
            continue;
        }
        if (str == "delete") {
            T.del(k);
        }
    }
    return 0;
}

Measured memory_bytes: 21832