#include<stdio.h>
#include<stdlib.h>

typedef struct node{
  struct node *parent;
  int key;
  struct node *left;
  struct node *right;
}Node;

Node *head;
void insert(Node *,int);
Node *make_node(Node *,int,Node *,Node *);
void inorder_print(Node *);
void preorder_print(Node *);
void free_node(Node *);
Node *find(Node *,int);
void delete(Node *);
void Search_suc(Node *);

int main(){
  int n,key;
  char command[7];
  head = make_node(NULL,key,NULL,NULL);
  scanf("%d",&n);
  while(n--){
    scanf("%s",command);
    if(command[0] == 'i'){
     
      scanf("%d",&key);
      insert(head->left,key);
    }
    else if(command[0] == 'f'){
      scanf("%d",&key);
      if(find(head->left,key))printf("yes\n");
      else printf("no\n");
    }
    else if(command[0]=='d'){
      scanf("%d",&key);
      delete(find(head->left,key));
    }
    else{
      inorder_print(head->left);
      printf("\n");
      preorder_print(head->left);
      printf("\n");
    }
  }
  if(head->left !=NULL)free_node(head->left);
  free(head);
}

void insert(Node *node,int in_key){
  if(node == NULL)head->left = make_node(node,in_key,NULL,NULL);
  else{
    if(node->key > in_key){
      if(node->left == NULL) node->left = make_node(node,in_key,NULL,NULL);
      else insert(node->left,in_key);
    }
    else{
      if(node->right == NULL) node->right = make_node(node,in_key,NULL,NULL);
      else insert(node->right,in_key);
    }
  }
}

Node *make_node(Node *p,int k,Node *l,Node *r){
  Node *new;

  new = malloc(sizeof(Node));
  if(new == NULL){
    free(new);
    printf("node Generation failed\n");
    exit(1);
  }
  new->parent = p;
  new->key = k;
  new->left = l; 
  new->right= r;
  return new;
}
void inorder_print(Node *next){
  if(next != NULL){
    inorder_print(next->left);
    printf(" %d",next->key);
    inorder_print(next->right);
  }
}
void preorder_print(Node *next){
  if(next != NULL){
    printf(" %d",next->key);
    preorder_print(next->left);
    preorder_print(next->right);
  }
}

Node *find(Node *n,int k){
  if(n->key == k)return n;
  else if(n->key > k){
    if(n->left != NULL)return find(n->left,k);
    else return NULL;
  }
  else if(n->key < k){
    if(n->right != NULL)return find(n->right,k);
    else return NULL;
  }
  else return NULL;
}
 
void delete(Node *del){
  Node *node;
  if(del->left == NULL && del->right == NULL){
    node = del->parent;
    if(node->left == del)node->left = NULL;
    else node->right = NULL;
    free(del);
  }
  else if(del->left == NULL){
    node = del->parent;
    if(node->left == del) node->left = del->right;
    else node->right = del->right;
    del->right->parent = del->parent;
    free(del);
  }
  else if(del->right == NULL){
    node = del->parent;
    if(node->left == del) node->left = del->left;
    else node->right = del->left;
    del->left->parent = del->parent;
    free(del);
  }
  else Search_suc(del);   
  
}

void Search_suc(Node *del){
  Node *node1;
  
    node1 = del->right;
    while(node1->left != NULL)node1 = node1->left;
    del->key = node1->key;
    delete(node1);
}    

void free_node(Node *n){
  if(n->left != NULL){
    free_node(n->left);
    free(n->left);
  }
  if(n->right != NULL){
    free_node(n->right);
    free(n->right);
  }
}



Measured memory_bytes: 20484