#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
  
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
  
using namespace std;
  
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
  
const int INF=1<<29;
const double EPS=1e-9;
  
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
 
struct node {
    int key;
    node *left;
    node *right;
    node *parent;
    node(int k) : key(k), left(nullptr), right(nullptr), parent(nullptr){
    }
};
 
void insert(node *to, node *v) {
    if (v->key < to->key) {
        if (to->left != nullptr) {
            insert(to->left, v);
        } else {
            v->parent = to;
            to->left = v;
        }
    } else {
        if (to->right != nullptr) {
            insert(to->right, v);
        } else {
            v->parent = to;
            to->right = v;
        }
    }
}
 
node *find(node *to, node *v) {
    if (to->key == v->key) {
        return to;
    }
    node *ret = nullptr;
    if (v->key < to->key) {
        if (to->left != nullptr) {
            ret = find(to->left, v);
        }
    } else {    
        if (to->right != nullptr) {
            ret = find(to->right, v);
        }
    }
    return ret;
}

void deletenode(node *target) {
    
    if (target->left == nullptr && target->right == nullptr) {
        if (target->parent->left == target) {
            target->parent->left = nullptr;
        } else {
            target->parent->right = nullptr;
        }
        delete target;
    } else if (target->left == nullptr) {
        if (target->parent->left == target) {
            target->parent->left = target->right;
        } else {
            target->parent->right = target->right;
        }
        target->right->parent = target->parent;
        delete target;
    } else if (target->right == nullptr) {
        if (target->parent->left == target) {
            target->parent->left = target->left;
        } else {
            target->parent->right = target->left;
        }
        target->left->parent = target->parent;
        delete target;
    } else {
        node *nextmid = target->right;
        while(nextmid->left != nullptr) {
            nextmid = nextmid->left;
        }
        target->key = nextmid->key;
        deletenode(nextmid);
    }

}
 
void print_first(node *target) {
    cout << " " << target->key;
    if (target->left != nullptr) print_first(target->left);
    if (target->right != nullptr) print_first(target->right);
}
void print_mid(node *target) {
    if (target->left != nullptr) print_mid(target->left);
    cout << " " << target->key;
    if (target->right != nullptr) print_mid(target->right);
}
 
void deletetree(node *target) {
    if (target->left != nullptr) deletetree(target->left);
    if (target->right != nullptr) deletetree(target->right);
    delete target;
}
 
int main() {
    int N;
    int num;
    string inst;
 
    cin >> N;
     
    cin >> inst;
    cin >> num;
    N--;
 
    node *root = new node(num);
    for (int i = 0; i < N; ++i) {
        cin >> inst;
        if (inst == "insert") {
            cin >> num;
            node *v = new node(num);
            insert(root, v);
 
        } else if (inst == "print") {
            print_mid(root);
            cout << endl;
            print_first(root);
            cout << endl;
        } else if (inst == "find") {
            cin >> num;
            node *v = new node(num);
            if (find(root, v) != nullptr) {
                cout << "yes" << endl;
            } else {
                cout << "no" << endl;
            }
        } else if (inst == "delete") {
            cin >> num;
            node *v = new node(num);
            node *target = find(root, v);
            deletenode(target);
        }
    }
    deletetree(root);
     
}

Measured memory_bytes: 24656