#include <bits/stdc++.h>
using namespace std;
int main(){
  int m;
  cin >> m;
  vector<int> nodes;
  vector<pair<int, int>> child;
  int n = 0;
  for (int i = 0; i < m; i++){
    string q;
    cin >> q;
    if (q == "insert"){
      int k;
      cin >> k;
      if (n != 0){
        int v = 0;
        while (1){
          if (k < nodes[v]){
            if (child[v].first != -1){
              v = child[v].first;
            } else {
              child[v].first = nodes.size();
              break;
            }
          } else {
            if (child[v].second != -1){
              v = child[v].second;
            } else {
              child[v].second = nodes.size();
              break;
            }
          }
        }
      }
      nodes.push_back(k);
      child.push_back(make_pair(-1, -1));
      n++;
    }
    if (q == "find"){
      int k;
      cin >> k;
      if (n == 0){
        cout << "no" << endl;
      } else {
        int v = 0;
        while (1){
          if (k < nodes[v]){
            if (child[v].first == -1){
              cout << "no" << endl;
              break;
            } else {
              v = child[v].first;
            }
          } else if (k > nodes[v]){
            if (child[v].second == -1){
              cout << "no" << endl;
              break;
            } else {
              v = child[v].second;
            }
          } else {
            cout << "yes" << endl;
            break;
          }
        }
      }
    }
    if (q == "delete"){
      int k;
      cin >> k;
      int v = 0;
      int u;
      while (1){
        if (k < nodes[v]){
          u = v;
          v = child[v].first;
        } else if (k > nodes[v]){
          u = v;
          v = child[v].second;
        } else {
          break;
        }
      }
      if (child[v].first != -1 && child[v].second != -1){
        u = v;
        int w = child[v].second;
        while (child[w].first != -1 || child[w].second != -1){
          u = w;
          if (child[w].first != -1){
            w = child[w].first;
          } else {
            w = child[w].second;
          }
        }
        swap(nodes[v], nodes[w]);
        v = w;
      }
      if (child[v].first != -1 && child[v].second == -1){
        int w = child[v].first;
        nodes[v] = nodes[w];
        child[v].first = child[w].first;
        child[v].second = child[w].second;
      } else if (child[v].second != -1 && child[v].first == -1){
        int w = child[v].second;
        nodes[v] = nodes[w];
        child[v].first = child[w].first;
        child[v].second = child[w].second;
      } else {
        if (child[u].first == v){
          child[u].first = -1;
        } else {
          child[u].second = -1;
        }
      }
      n--;
    }
    if (q == "print"){
      //Inorder
      if (n != 0){
        stack<int> S;
        vector<int> t(nodes.size(), 0);
        S.push(0);
        while (!S.empty()){
          int v = S.top();
          S.pop();
          if (t[v] == 1){
            cout << ' ' << nodes[v];
          }
          if (t[v] == 0){
            S.push(v);
            if (child[v].second != -1){
              S.push(child[v].second);
            }
            S.push(v);
            if (child[v].first != -1){
              S.push(child[v].first);
            }
          }
          t[v]++;
        }
      }
      cout << endl;
      //Preorder
      if (n != 0){
        stack<int> S;
        vector<int> t(nodes.size(), 0);
        S.push(0);
        while (!S.empty()){
          int v = S.top();
          S.pop();
          if (t[v] == 0){
            cout << ' ' << nodes[v];
          }
          if (t[v] == 0){
            S.push(v);
            if (child[v].second != -1){
              S.push(child[v].second);
            }
            S.push(v);
            if (child[v].first != -1){
              S.push(child[v].first);
            }
          }
          t[v]++;
        }
      }
      cout << endl;
    }
  }
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;


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


struct Tree{
    Node *nil,*root;

    void init(){
        root=nil;
    }

    void add(ll key){
        Node *newnode;
        Node *p;
        newnode = (Node*)malloc(sizeof(Node));

        newnode->key = key;
        newnode->parent = newnode->right = newnode->left = nil;

        Node *pa = find_node(key);
        if(pa == nil){
            root = newnode;
        }else if(key < pa->key){
            pa->left = newnode;
            newnode->parent = pa;
        }else{
            pa->right = newnode;
            newnode->parent = pa;
        }
    }

    Node *find_node(ll key){
        Node *pa=nil;
        Node *loc=root;
        while(loc != nil){
            pa = loc;
            if(key < loc->key){
                loc = loc->left;
            }else{
                loc = loc->right;
            }
        }
        return pa;
    }

    Node *find_x(ll x){
        Node *p=root;
        while(p != nil){
            if(x == p->key){
                break;
            }else if(x < p->key){
                p = p->left;
            }else{
                p = p->right;
            }
        }
        return p;
    }


    ll find_rightmin(Node *r){
        Node *rmin = r->right;
        while(rmin->left != nil){
            rmin = rmin->left;
        }
        return rmin->key;
    }

    void del(ll k){
        Node *loc=find_x(k);
        Node *p=loc->parent;
        if(loc->left==nil && loc->right == nil){
            //leafだったら
            if(p==nil)root=nil;
            else if(p->key > loc->key)p->left=nil;
            else p->right=nil;
            free(loc);
        }else if(loc->left == nil || loc->right==nil){
            //片方だったら
            if(loc->left == nil){
                if(p == nil)root=loc->right, loc->right->parent=root;
                else if(p->key > loc->key)p->left=loc->right, loc->right->parent=p;
                else p->right=loc->right, loc->right->parent=p;
            }else{
                if(loc->parent == nil)root=loc->left, loc->left->parent=nil;
                else if(p->key > loc->key)p->left=loc->left, loc->left->parent=p;
                else p->right=loc->left, loc->left->parent=p;
            }
            free(loc);
        }else{
            //二分木の根だったら
            ll rmin=find_rightmin(loc);
            del(rmin);
            loc->key=rmin;
        }
    }


    void print_in(Node *r){
        if(r==nil)return;
        else{
            print_in(r->left);
            cout << " " << r->key;
            print_in(r->right);
        }
    }

    void print_pre(Node *r){
        if(r==nil)return;
        else{
            cout << " " << r->key;
            print_pre(r->left);
            print_pre(r->right);
        }
    }
    void print(){
        print_in(root);
        cout << endl;
        print_pre(root);
        cout << endl;
    }
};


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

    Tree tree;
    tree.init();
    int n;
    cin >> n;
    string s;
    for(int i=0;i<n;i++){
        cin >> s;
        if(s=="insert"){
            ll k;
            cin >> k;
            tree.add(k);
        }else if(s=="find"){
            ll k;
            cin >> k;
            Node *p = tree.find_x(k);
            if(p != tree.nil)cout << "yes\n";
            else cout << "no\n";
        }else if(s=="delete"){
            ll k;
            cin >> k;
            tree.del(k);
        }else {
            tree.print();
        }
    }
    return 0;
}

