#include <iostream>
#include <cstdlib>

using namespace std;

typedef struct node {
    int key;
    struct node *parent;
    struct node *left;
    struct node *right;
} Tree;
 
Tree *root, *nil;
 
void insert(int key);
Tree *findNode(Tree *node, int key);
bool isFindTree(Tree *node, int key);
Tree *findMaximumTree(Tree *node);
Tree *treeSuccessor(Tree *node);
void treeDelete(Tree *z);
void preorder(Tree *node);
void inorder(Tree *node);

int main() {
    int n, key, i;
    string command;
    cin >> n;
    for (i = 0; i < n; i++) {
        cin >> command;
        if (command[0] == 'i') {
            cin >> key;
            insert(key);
        }
        else if (command[0] == 'd') {
            cin >> key;
            treeDelete(findNode(root, key));
        }
        else if (command[0] == 'f') {
            cin >> key;
            if (isFindTree(root, key)) {
                cout << "yes" << endl;
            } else {
                cout << "no" << endl;
            }
        }
        else if (command[0] == 'p') {
            inorder(root); cout << endl;
            preorder(root); cout << endl;
        }
    }

    return 0;
}

void insert(int key) {
    Tree *y = nil;
    Tree *x = root;
    Tree *z = (struct node*)malloc(sizeof(struct node));
    z->key = key;
    z->left = nil;
    z->right = nil;
    while (x != nil) {
        y = x;
        if (key < x->key) {
            x = x->left;
        } else {
            x = x->right;
        }
    };
    z->parent = y;
    if (y == nil) {
        root = z;
        return;
    }
    if (key <= y->key) {
        y->left = z;
    } else {
        y->right = z;
    }
}


Tree *findNode(Tree *node, int key) {
    Tree* target = node;
    while (target != nil && target->key != key) {
        if (key < target->key) {
            target = target->left;
        } else {
            target = target->right;
        }
    }
    return target;
}

bool isFindTree(Tree *node, int key) {
    return findNode(node, key) != nil;
}

Tree *findMaximumTree(Tree *node) {
    while (node->left != nil) {
        node = node->left;
    }
    return node;
}

Tree *treeSuccessor(Tree *node) {
    if (node->right != nil) {
        return findMaximumTree(node->right);
    }
    Tree *y = node->parent;
    while (y != nil && node == y->right) {
        node = y;
        y = y->parent;
    }
    return y;
}
 
void treeDelete(Tree *z) {
    Tree *y, *key;
    if (z->left == nil || z->right == nil) y = z;
    else y = treeSuccessor(z);
    if (y->left != nil) {
        key = y->left;
    } else {
        key = y->right;
    }
    if (key != nil) {
        key->parent = y->parent;
    }
    if (y->parent == nil) {
        root = key;
    } else {
        if (y == y->parent->left) {
            y->parent->left = key;
        } else {
            y->parent->right = key;
        }
    }
    if (y != z) {
        z->key = y->key;
    }
}

void preorder(Tree *node) {
    if (node == nil) return;
    cout << " " << node->key;
    preorder(node->left);
    preorder(node->right);
}

void inorder(Tree *node) {
    if (node == nil) return;
    inorder(node->left);
    cout << " " << node->key;
    inorder(node->right);
}



#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

#define N 600000
#define NIL -1

//array
int key[N];
int parent[N];
int leftc[N];
int rightc[N];
//root
int root=NIL;

//function insert
void insert(int z){
  int x,y;
  
  y=NIL;
  x=root;

  while(x!=NIL){
    y=x;
    if(key[z]<key[x]) x=leftc[x];
    else x=rightc[x];
  }  
  
  parent[z]=y;

  if(y==NIL) root=z;
    
  else if(key[z]<key[y]) leftc[y]=z;

  else rightc[y]=z;
  
  
}

//function search
int find(int now,int data){
  if(now==NIL ||data==key[now]) return now;
  
  if(data<key[now]) return find(leftc[now],data);
  else return find(rightc[now],data);
}


int treeminimum(int x){
  while(leftc[x]!=NIL){
    x=leftc[x];
  }
  return x;
}


int treesuccessor(int x){
  int y; 
  if(rightc[x]!=NIL) return treeminimum(rightc[x]);
  y=parent[x];
  while(y!=NIL &&x==rightc[x]){
    x=y;
    y=parent[y];
  }
  return y;
}


//function delete
void treedelete(int z){
  int x,y;
  if(leftc[z]==NIL ||rightc[z]==NIL) y=z;
  else y=treesuccessor(z);

  if(leftc[y]!=NIL) x=leftc[y];
  else x=rightc[y];

  if(x!=NIL) parent[x]=parent[y];
  if(parent[y]==NIL) root=x;
  else if(y==leftc[parent[y]]) leftc[parent[y]]=x;
  else rightc[parent[y]]=x;

  if(y!=z) key[z]=key[y];

}

//print
void inorderprint(int now){
  if(now!=NIL){
    inorderprint(leftc[now]);
    printf(" %d",key[now]);
    inorderprint(rightc[now]);
  }
}

void preorderprint(int now){
  if(now!=NIL){
    printf(" %d",key[now]);
    preorderprint(leftc[now]);
    preorderprint(rightc[now]);
  }
}



int main(){

  int n,i,j,data;
  int z=0;
  int del;
  char str[10];
  //initialize array
  for(i=0;i<N;i++){
    parent[i]=NIL;
    key[i]=NIL;
    rightc[i]=NIL;
    leftc[i]=NIL;
  }
  //load n
  scanf("%d",&n);
  //load commnad and key
  for(i=0;i<n;i++){
    scanf("%s",str);
    if(strcmp(str,"insert")==0) {
      scanf("%d",&data);
      key[z]=data;
      insert(z);
      z++;
    }
    else if(strcmp(str,"find")==0){
      scanf("%d",&data);
      if(find(root,data)!=NIL) printf("yes\n");
      else printf("no\n");
    }
    else if(strcmp(str,"delete")==0){
      scanf("%d",&data);
      del=find(root,data);
      treedelete(del);
    }
    else if(strcmp(str,"print")==0){
      inorderprint(root);
      cout <<endl;
      preorderprint(root);
      cout <<endl;
    }
  }

  return 0;
}