#include<stdio.h>
#include<stdlib.h>
#include<string.h>
  
struct Node{
  
  int key;
  struct Node *parent;
  struct Node *left;
  struct Node *right;
  
};
  
struct Node *root,*NIL;
void insert(int );
void preorder(struct Node *);
void inorder(struct Node *);
struct Node *successor(struct Node*);
struct Node *find(struct Node *, int);
void deleteNode( struct Node *);
struct Node * miniNode(struct Node*);
int main(){
  
  int sumnode,inputdata,i,findnumber;
  char string[10];
  int deletenum;
  struct Node *deletenode;
  struct Node *answer;

  scanf("%d",&sumnode);
  
  for(i = 0; i < sumnode; i++){
  
    scanf("%s",string);
  
    if((strcmp(string,"insert")) == 0){
  
      scanf("%d",&inputdata);
      insert(inputdata);
  
    }
  
  
    else if((strcmp(string,"print"))==0){
  
      inorder(root);
  
      printf("\n");
  
      preorder(root);
  
      printf("\n");
     
          
    }
  
    else if((strcmp(string,"delete")) == 0){

      scanf("%d",&deletenum);

      deleteNode(find(root,deletenum));

    }

    else if((strcmp(string,"find")) == 0){
  
  
      struct Node *answer;
      scanf("%d",&findnumber);
  
      answer =  find(root,findnumber);

   
   if(answer !=NIL){
  
     printf("yes\n");
  
   }
  
   else{
  
     printf("no\n");
  
   }
    }

  }
 
 return 0;
}

void insert(int inputdata){
  
  struct Node *correntNode; 
  struct Node *beforeNode; 
  struct Node *newNode;
  
  newNode = (struct Node*)malloc(sizeof(struct Node));
  
  newNode -> key = inputdata;
  
  newNode->parent = NIL;
  
  newNode->left = NIL;
  
  newNode->right = NIL;
  
  correntNode = root;
  beforeNode = NIL;
  
  while(correntNode != NIL){
  
    beforeNode = correntNode;
  
    if(newNode->key < correntNode->key){
  
      correntNode = correntNode->left;
  
    }
  
    else{
  
      correntNode =  correntNode->right;
  
    }
  } 
  
  newNode->parent = beforeNode;
  
  
  if(beforeNode == NIL){ 
  
    root = newNode;;
  
  }
  
  else if(newNode->key > beforeNode->key){
  
    beforeNode->right = newNode;
  
  }
  
  else{
  
    beforeNode->left = newNode;
  
  }
  
}
  
void preorder(struct Node *inputdata){
  
  if(inputdata != NIL){
  
    printf(" %d",inputdata->key);
  
    preorder(inputdata->left);
    preorder(inputdata->right);
  
  }
  
}
  
void inorder(struct Node *inputdata){
  
  if(inputdata != NIL){
  
    inorder(inputdata->left);
  
    printf(" %d",inputdata->key);
  
    inorder(inputdata->right);
  
  }
  
}
  
  
struct Node * find(struct Node *inputdata ,int findnumber){
  
  int flag = 0;
  
  while( inputdata != NIL && inputdata->key != findnumber){
  
    if(inputdata->key < findnumber){
  
      inputdata = inputdata->right;
  
    }
  
    else if(inputdata->key > findnumber){
  
      inputdata = inputdata->left;
  
    }
  }
  
  
  
  return inputdata;
  
}

 struct Node *Successor(struct Node *inputdata){

   struct Node *y;
   if(inputdata->right != NIL){

     return miniNode(inputdata->right);

   }

   y = inputdata->parent;

   while(y != NIL && inputdata == y->right){

     inputdata = y;

     y = y->parent;

   }
    
   
   return y;

 }

 void deleteNode(struct Node *deletenode){

   struct Node *x,*y;

   if(deletenode->left == NIL || deletenode->right == NIL){

     y = deletenode;

   }

   else{

     y = Successor(deletenode);

   }

   if(y->left != NIL){

     x = y->left;

   }

   else{

     x = y->right;

   }

   if(x != NIL){

     x->parent = y->parent;

   }

   if(y->parent == NIL){

     root = x;

   }

   else{

     if(y == y->parent->left){
     
       y->parent->left = x;

     }

   else{

     y ->parent->right = x;

   }

   }

   if(y != deletenode){

     deletenode->key = y->key;

   }

 }

struct Node* miniNode(struct Node *inputdata){

  while(inputdata->left != NIL){

    inputdata = inputdata->left;

  }

  return inputdata;

}

Measured memory_bytes: 19364