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

struct Node
{
    int key;
    struct Node * parent;
    struct Node * left;
    struct Node * right;
    Node(int key_) : key(key_), parent(nullptr), left(nullptr), right(nullptr) {};
};

struct BinarySearchTree
{
    int n_node;
    Node * root;
    
    // Constructor
    BinarySearchTree(): n_node(0), root(nullptr) {};
    
    // Insert
    void insert(int key){
        Node * z = new Node{key};
        Node * y = nullptr;
        Node * x = root;
        while (x){
            y = x;
            x = ((key < x->key) ? x->left : x->right);
        }
        z->parent = y;
        if (!y){
            root = z;
        }else if (key < y->key){
            y->left = z;
        }else{
            y->right = z;
        }
        ++n_node;
    }
    
    // Find
    Node * find(int key){
        auto x = root;
        while (x){
            if (key == x->key) return x;
            x = ((key < x->key) ? x->left : x->right);
        }
        return x;
    }
    
    // Delete
    void del(int key){
        auto x = find(key);
        if (x) _deleteNode(x);
    }

    void _deleteNode(Node * x){
        // count the number of children.
        int n_children = 0;
        auto L = x->left;
        auto R = x->right;
        if (L) ++n_children;
        if (R) ++n_children;
        auto p = x->parent;

        if (n_children == 0){
            ((p->left == x) ? p->left : p->right) = nullptr;
            --n_node;
            delete x;
        }else if (n_children == 1){
            auto c = (L ? L : R);
            ((p->left == x) ? p->left : p->right) = c;
            c->parent = p;
            --n_node;
            delete x;
        }else if (n_children == 2){
            // x ??? inorder walk ?????§????¬????????????? y ???????????????
            auto y = R;
            while (y->left) y = y->left;
            // y??????????????????x???????????????????????????y????????????
            x->key = y->key;
            _deleteNode(y);
        }
    }

    // print
    void print(){
        _inorder_walk(root);
        cout << endl;
        _preorder_walk(root);
        cout << endl;
    }
    
    //inorder walk
    void _inorder_walk(Node * node){
        if (!node) return;
        _inorder_walk(node->left);
        cout << ' ' << node->key;
        _inorder_walk(node->right);        
    }
    
    //preorder walk
    void _preorder_walk(Node * node){
        if (!node) return;
        cout << ' ' << node->key;
        _preorder_walk(node->left);
        _preorder_walk(node->right);
    }
    
    
};

int main()
{
    ios::sync_with_stdio(false);
    int m = 0;
    cin >> m;
    
    BinarySearchTree bst;
    string command;
    int key;
    for (int i = 0; i != m; ++i){
        cin >> command;
        if (command[0] == 'i'){
            cin >> key;
            bst.insert(key);
        }else if (command[0] == 'f'){
            cin >> key;
            cout << (bst.find(key) ? "yes" : "no") << endl;
        }else if (command[0] == 'd'){
            cin >> key;
            bst.del(key);
        }else if (command[0] == 'p'){
            bst.print();
        }else{
            cout << "unexpected command! " << command << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 21972