#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ORDER 500000

struct NODE{
  int key,nil;
  struct NODE *left,*right,*parent;
};

typedef struct NODE Node;

Node *head;

void insert(Node*);
void print1(Node*);
void print2(Node*);
int find(Node*,int);
void delete(Node*);
Node* successor(Node*);
Node* minimum(Node*);
Node* search(Node*,int);

int main(){
  int i,num;
  Node *n;
  char order[ORDER][10];
  Node tmp;

  scanf("%d",&num);

  n = (Node *)malloc(sizeof(Node) * num);

for(i=0; i<num; i++){
    scanf("%s",order[i]);
    if(strcmp(order[i],"print")!=0)scanf("%d",&n[i].key);
  }

  for(i=0; i<num; i++){
    if(strcmp(order[i],"insert")==0) insert(&n[i]);
    if(strcmp(order[i],"print")==0){
      print1(head);
      printf("\n");
      print2(head);
      printf("\n");
    }
    if(strcmp(order[i],"find")==0){
      if(find(head,n[i].key)==1) printf("yes\n");
      else printf("no\n");
    }
    if(strcmp(order[i],"delete")==0)
 delete(search(head,n[i].key));
  }

  return 0;
}






void insert(Node *z){
  Node *y,*x;

 z->left = NULL;
  z->right = NULL;
  y = NULL;
  x = head;

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

  if(y == NULL) head = z;
  else if(z->key < y->key) y->left = z;
  else y->right = z;
}



void print1(Node *node){
  if(node == NULL){
    return;
  }

  print1(node->left);
  printf(" %d",node->key);
  print1(node->right);
  return ;
}



void print2(Node *node){
  if(node == NULL){
    return;
  }
  printf(" %d",node->key);
  print2(node->left);
  print2(node->right);
  return;
}



int find(Node *node,int num){
  int found=0;
  if(node == NULL){
    return 0;
  }
  if(node->key == num) found=1;
  if(node->key > num) found = find(node->left,num);
  if(node->key < num) found = find(node->right,num);

  return found;
}



void delete(Node *node){
  Node *x,*y;
  if(node->left == NULL || node->right == NULL) y = node;
  else y = successor(node);

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

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

  if(y->parent == NULL) head = x;
  else if(y == y->parent->left) y->parent->left = x;
  else y->parent->right = x;

  if(y != node) node->key = y->key;
}



Node* successor(Node *node){
  Node *y,*x;
  if(node->right != NULL) return minimum(node->right);
  x = 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;
}


Node* search(Node *node,int num){
  Node *found;
  if(node == NULL){
    return 0;
  }
  if(node->key == num){
    found = node;
  }
  if(node->key > num) found = search(node->left,num);
  if(node->key < num) found = search(node->right,num);

  return found;
}