#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 <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct node{
    int key;
    node *parent,*left,*right;
};
node *NIL,*root;
 
void inorder(struct node *u){
    if(u==NIL)return;
    inorder(u->left);
    printf(" %d",u->key);
    inorder(u->right);
}
void preorder(struct node *u){
    if(u==NIL)return;
    printf(" %d",u->key);
    preorder(u->left);
    preorder(u->right);
}
void pr(){
    inorder(root);
    printf("\n");
    preorder(root);
    printf("\n");
}
void insert(int key){
    node *y = NIL;
    node *x = root;
    node *z;
    z = (node *)malloc(sizeof(node));
    z->key = key;
    z->left = NIL;
    z->right = NIL;
     
    while(x!=NIL){//when x==NIL break
        y = x;
        if(z->key < x->key)x = x->left;
        else x = x->right;
    }
    z->parent = y;
    if(y == NIL)root = z;//z became root,insert root
    else if(z->key < y->key)y->left = z;
    else y->right = z;
}
node * find(node *u,int k){
    while((u != NIL) && (k != u->key)){  
    //when you exchange the position of '(u != NIL)' with '(k != u->key)',the program will run failed
    //you must make sure that u!=NIl firstly,otherwise k will compare with NIL->key(which is not defined)
        if(k < u->key)u = u->left;
        else u = u->right;
    }
    return u;
}
node * treeMinmum(node *x){
	while(x->left!=NIL)x = x->left;//"leftmost"
	return x;
}
node * treeSuccessor(node *x){
	if(x->right != NIL)return treeMinmum(x->right);//exist right tree,find the next node inorder
	
	node *y = x->parent;//not exist,return parent node which acts as left-child node firstly
	while(y!=NIL && y->right == x){
		x = y;
		y = y->parent;
	}
	return y;
}
void treedelete(node *z){
	node *y;//the node to delete
	node *x;//child-node of the node needed to be deleted
	
	//make sure wheter z delete or not
	if(z->left == NIL || z->right == NIL)y = z;
	else y = treeSuccessor(z);
	
	if(y->left != NIL)x = y->left;
	else x = y->right;
	
	if(x != NIL )x->parent = y->parent; // make y->parent as x new parent
	
	// make x as y->parent new child
	if(y->parent == NIL)root = x;//y was root
	else if(y == y->parent->left)y->parent->left = x;
	else y->parent->right = x;
	
	
	if(y!=z)z->key = y->key;
	free(y);
}
int main()
{
    int n,k;
    scanf("%d",&n);
    char s[20];
    for(int i=0;i<n;++i){
        scanf("%s",s);
        if(s[0]=='i'){
            scanf("%d",&k);
            insert(k);
        }else if(s[0]=='f'){
            scanf("%d",&k);
            node *t = find(root,k);
            if(t != NIL )printf("yes\n");
            else printf("no\n");
        }
        else if(s[0]=='d'){
        	scanf("%d",&k);
        	treedelete(find(root,k));
		}
		else pr();
    }
    return 0;
}