#include<bits/stdc++.h>
#include<limits.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define MAX 100000

struct node{
    int key;
    node* parent;
    node* left;
    node* right;
};

node *nil, *root;

void insert(int key){

    node *y = nil;          // ノードyはzの親
    node *x = root;    // ノードxは木の根をさす
    node *z = (node *)malloc(sizeof(node));
    
    // 挿入ノードの初期値
    z->key = key;
    z->left = nil;
    z->right = nil;

    //　挿入先ノード探索
    while(x != nil){        // xがnilを指している(初期ノード)のとき終了
        y = x;              // zの親がxであるとする。
        
        if(z->key < x->key){// xのキーよりzのキーがxを小さければ左、そうでなければ右へ
            x = x->left;
        }else{
            x = x->right;
        }
    }

    z->parent = y;

    if(y == nil){ // treeが空の時
        root = z; 
    }else{
        if(z->key < y->key){
            y->left = z;
        }else{
            y->right = z;
        }
    }
}

node * find(int key){
    node *x = root;
    while(x != nil){
        if(x->key < key){
            x = x->right;
        }else if(x->key > key){
            x = x->left;
        }else{
            return x;
        }
    }
    return nil;
}

node * get_most_left_node(node *x){
    node * y = x->right;
    while(y->left != nil){
        y = y-> left;
    }
    return y;
}

void deleteNode(node * delete_node){

    if(delete_node->left == nil && delete_node->right == nil){
        //子が一つもない場合
        if(delete_node->parent->key > delete_node->key){
            delete_node->parent->left = nil;
        }else{
            delete_node->parent->right = nil;
        }
        free(delete_node);

    }else if(delete_node->left != nil && delete_node->right != nil){
        //子が二つの場合
        node *most_left = get_most_left_node(delete_node);
        //most_leftノードをdelete_nodeと入れ替え
        // まずはmost_leftノードの情報を退避し、most_leftノードを除去。
        // その後、退避しておいたノード情報によってdelete_nodeを入れ替え
        int k = most_left->key;

        deleteNode(most_left);

        delete_node->key = k;
    }else{
        //子ノードが一つの場合
        if(delete_node->left != nil){
            delete_node->left->parent = delete_node->parent;
            
            if(delete_node->key < delete_node->parent->key){
                delete_node->parent->left = delete_node->left;
            }else{
                delete_node->parent->right = delete_node->left;
            }
        }else{
            delete_node->right->parent = delete_node->parent;
            
            if(delete_node->key < delete_node->parent->key){
                delete_node->parent->left = delete_node->right;
            }else{
                delete_node->parent->right = delete_node->right;
            }
                
        }
        free(delete_node);
    }
}

void preorder(node *x){
    if(x == nil){
        return;
    }
    cout << " " << x->key;
    preorder(x->left);
    preorder(x->right);
}

void inorder(node *x){
    if(x == nil){
        return;
    }
    inorder(x->left);
    cout << " " << x->key;
    inorder(x->right);
}

int main(){
    int n; cin >> n;
    
    
    rep(i, n){
        string command; cin >> command;

        if(command == "insert"){
            int x; cin >> x;
            insert(x);

        }else if(command == "find"){
            int x; cin >> x;
            node *tmp = find(x);
            if(tmp != nil){
                cout << "yes" << endl;
            }else{
                cout << "no" << endl;
            }
        }else if(command == "delete"){
            int x; cin >> x;
            deleteNode(find(x));

        }else if(command == "print"){
            inorder(root);
            cout << endl;
            preorder(root);
            cout << endl;
        }
    }

    return 0;
}


Measured memory_bytes: 21952