#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ld> vd;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll;  // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll;     // 大きい順に取り出し

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); i++)
#define FI first
#define SE second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define TS to_string
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)


ll INF = 1e9;
ll MOD = 1000000007;
ll LINF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b;}     //最小公倍数

void yes(){ cout << "yes" << endl;}
void no(){ cout << "no" << endl;}

//-----------------------------------------
template<class T>
class BT{
  private:
    struct Node{
      T key;
      Node *par, *lef, *rig;
    };
    Node *NIL, *root;
  public:
    BT(){
      NIL = NULL;
    }
    void insert(T k){
      Node *y = NIL;
      Node *x = root;
      Node *z;

      z = (Node*)malloc(sizeof(Node));
      z->key = k;
      z->lef = NIL;
      z->rig = NIL;

      while(x != NIL){
        y = x;
        if(k < x->key) x = x->lef;
        else x = x->rig;
      }
      z->par = y;
      if(y == NIL) root = z;
      else if(z->key < y->key) y->lef = z;
      else y->rig = z;
    }

    Node *find_node(T k, Node *t){
      if(t == NIL) return NIL;
      if(t->key == k) return t;
      if(k < t->key) return find_node(k,t->lef);
      else return find_node(k,t->rig);
    }
    bool find(T k){
      Node *t = find_node(k,root);
      if(t == NIL) return false;
      else return true;
    }

    Node *mini_t(Node *t){
      while(t->lef != NIL) t = t->lef;
      return t;
    }
    T mini(Node *t){
      return mini_t(root)->key;
    }


    void del(T k){
      Node *tmp = find_node(k,root);
      if(tmp == NIL) return;

      Node *p = tmp->par;
      if(tmp->lef == NIL && tmp->rig == NIL){
        if(p->lef == tmp) p->lef = NIL;
        else p->rig = NIL;
        free(tmp);
      }
      else if(tmp->lef == NIL){
        if(p->lef == tmp){
          p->lef = tmp->rig;
          tmp->rig->par = p;
        }
        else{
          p->rig = tmp->rig;
          tmp->rig->par = p;
        }
        free(tmp);
      }
      else if(tmp->rig == NIL){
        if(p->lef == tmp){
          p->lef = tmp->lef;
          tmp->lef->par = p;
        }
        else{
          p->rig = tmp->lef;
          tmp->lef->par = p;
        }
        free(tmp);
      }
      else{
        Node *m = mini_t(tmp->rig);
        Node *q = m->par;
        tmp->key = m->key;
        if(m->rig == NIL){
          if(q->lef == m){
            q->lef = NIL;
          }
          else{
            q->rig = NIL;
          }
        }
        else{
          if(q->lef == m){
            q->lef = m->rig;
            m->rig->par = q;
          }
          else{
            q->lef = m->rig;
            m->lef->par = q;
          }
        }
        free(m);
      }
    }

    void pmid(Node *t){
      if(t == NIL) return;
      pmid(t->lef);
      cout << " " << t->key;
      pmid(t->rig);
    }

    void ppre(Node *t){
      if(t == NIL) return;
      cout << " " << t->key;
      ppre(t->lef);
      ppre(t->rig);
    }

    void pr(){
      pmid(root);
      cout <<endl;
      ppre(root);
      cout <<endl;
    }
};
//-----------------------------------------

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  BT<int> tree;
  REP(i,n){
    string s;
    cin >> s;
    if(s[0] == 'i'){
      int a;
      cin >> a;
      tree.insert(a);
    }
    else if(s[0] == 'f'){
      int a;
      cin >> a;
      if(tree.find(a)) yes();
      else no();
    }
    else if(s[0] == 'd'){
      int a;
      cin >> a;
      tree.del(a);
    }
    else{
      tree.pr();
    }
  }



  return 0;
}






Measured memory_bytes: 21920