#include <bits/stdc++.h>
#include <cmath>

using namespace std;
typedef long long ll;
//typedef unsigned long long ll;

//#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
//#define sz(x) ll(x.size())
//typedef pair<ll, int> P;
//typedef pair<ll, ll> P;
//const double INF = 1e10;
//const ll INF = LONG_LONG_MAX / 10;
//const ll INF = 1e15;
const ll MINF = LONG_LONG_MIN;
const int INF = INT_MAX / 10;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)

bool contain(set<char> &s, int a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

typedef pair<int, int> P;

void print_line(vector<int> &line) {
    if (line.size() == 0ll) {
        cout << endl;
        return;
    }
    for (ll i = 0; i < line.size(); i++) {
        cout << line[i];
        if (i == line.size() - 1) cout << endl;
        else cout << ' ';
    }
}


typedef priority_queue<long long, vector<long long>, greater<long long> > PQ_ASK;
const int mod = 1000000007;

struct Node {
    ll key;
    Node *left, *right, *parent;
};

class Dict {
    Node *begin = NULL;

public:
    void insert(ll k) {
        Node *y = NULL;
        Node *x = begin;
        Node *z = new Node();


        z->key = k;
        z->parent = NULL;
        z->right = NULL;
        z->left = NULL;

        if (x == NULL) {
            begin = z;
            return;
        }

        while (x != NULL) {
            y = x;
            if (z->key < x->key) {
                x = x->left;
            } else {
                x = x->right;
            }
        }
        z->parent = y;
        if (y == NULL) {
            begin = z;
        } else {
            if (z->key < y->key) {
                y->left = z;
            } else {
                y->right = z;
            }
        }
    };

    Node *find(ll key) {
        Node *x = begin;
        while (x != NULL && key != x->key) {
            if (key < x->key) {
                x = x->left;
            } else {
                x = x->right;
            }
        }
        return x;
    }

    void del(ll key) {
        Node *z = find(key);
        if (z == NULL) return;

        Node *y;
        Node *x;

        if (z->left == NULL || z->right == NULL) y = z;
        else y = get_successor(z);

        if (y->left != NULL) {
            x = y->left;
        } else {
            x = y->right;
        }

        if (x != NULL) {
            x->parent = y->parent;
        }

        if (y->parent == NULL) {
            begin = 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() {
        _inorder(begin);
    }

    void preorder() {
        _preorder(begin);
    }

private:
    void _inorder(Node *node) {
        if (node == NULL) return;
        _inorder(node->left);
        cout << ' ' << node->key;
        _inorder(node->right);
    }

    void _preorder(Node *node) {
        if (node == NULL) return;
        cout << ' ' << node->key;
        _preorder(node->left);
        _preorder(node->right);
    }

    Node *get_successor(Node *x) {
        if (x->right != NULL) return minimum(x->right);
        Node *y = x->parent;
        while (y != NULL && x == y->right) {
            x = y;
            y = y->parent;
        }
        return y;
    }

    Node *minimum(Node *node) {
        while (node->left != NULL) node = node->left;
        return node;
    }

};

int main() {
    int n;
    cin >> n;

    Dict dict;

    rep(i, n) {
        string command;
        cin >> command;
        if (command == "insert") {
            ll k;
            cin >> k;
            dict.insert(k);
        } else if (command == "print") {
            dict.inorder();
            cout << endl;
            dict.preorder();
            cout << endl;
        } else if (command == "find") {
            ll k;
            cin >> k;
            if (dict.find(k) == NULL) {
                cout << "no" << endl;
            } else {
                cout << "yes" << endl;
            }
        } else if (command == "delete") {
            ll k;
            cin >> k;
            dict.del(k);
        }
    }

}




Measured memory_bytes: 21908