#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <clocale>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i)
#define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i)
#define VFOR(i, s, n) for(int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for(int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i)
#define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ll = long long;
using Pi_i = pair<int, int>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;

const int MOD = 1000000007;
const int INF = 1000000000;
const int NIL = -1;

template<class T>
bool chmax(T &a, const T &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T>
bool chmin(T &a, const T &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}




class BinarySearchTree{
    struct Node{
        int key;
        Node *right, *parent, *left;
        Node(): key(NIL), right(nullptr), parent(nullptr), left(nullptr){};
        Node(int k): key(k), right(nullptr), parent(nullptr), left(nullptr){};
    };
    Node *root, *NILL = nullptr;
public:
    BinarySearchTree(): root(nullptr){};

    void insert(int k){
        Node *y = nullptr;
        Node *x = root;
        Node *z = new Node(k);
        while(x != nullptr){
            y = x;
            if(z->key < x->key){
                x = x->left;
            }else{
                x = x->right;
            }
        }
        z->parent = y;
        if(y == nullptr)
            root = z;
        else{
            if(z->key < y->key){
                y->left = z;
            }else{
                y->right = z;
            }
        }
    }

private:
    Node* find(Node *v, int k){
        while(v != nullptr && k != v->key){
            if(k < v->key) v = v->left;
            else v = v->right;
        }
        return v;
    }

    Node* treeMinimum(Node *v){
        while(v->left != nullptr)
            v = v->left;
        return v;
    }

    Node* treeSuccesor(Node *v){
        if(v->right != nullptr) return treeMinimum(v->right);
        Node *y = v->parent;
        while(y != nullptr && v == y->right){
            v = y;
            y = y->parent;
        }
        return y;
    }

    void tDelete(Node *z){
        Node *y, *x;
        if(z->left == nullptr || z->right == nullptr)
            y = z;
        else y = treeSuccesor(z);
        if(y->left != nullptr){
            x = y->left;
        }else{
            x = y->right;
        }
        if(x != nullptr)
            x->parent = y->parent;
        if(y->parent == nullptr)
            root = x;
        else{
            if(y == y->parent->left)
                y->parent->left = x;
            else y->parent->right = x;
        }
        if(y != z){
            z->key = y->key;
        }
        delete(y);
    }

    void inOrder(Node *v){
        if(v == nullptr) return;
        inOrder(v->left);
        cout << " " << v->key;
        inOrder(v->right);
    }

    void preOrder(Node *v){
        if(v == nullptr) return;
        cout << " " << v->key;
        preOrder(v->left);
        preOrder(v->right);
    }
    
public:
    bool find(int x){
        Node *t = find(root, x);
        return t != nullptr;
    }

    void tDelete(int x){
        tDelete(find(root, x));
    }

    void outInOrder(){
        inOrder(root);
    }

    void outPreOrder(){
        preOrder(root);
    }

};


int main(){
    int n; cin >> n;
    BinarySearchTree T;
    REP(i, n){
        int x;
        string com; cin >> com;
        if(com == "insert"){
            cin >> x;
            T.insert(x);
        }
        else if(com == "find"){
            cin >> x;
            cout << (T.find(x) ? "yes" : "no") << endl;
        }
        else if(com == "delete"){
            cin >> x;
            T.tDelete(x);
        }
        else{
            T.outInOrder();
            cout << "\n";
            T.outPreOrder();
            cout << "\n";
        }
    }
    return 0;
}



Measured memory_bytes: 21760