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

/* Struct Declaration */
struct node {
  int key;
  struct node *parent;
  struct node *left;
  struct node *right;
};

typedef struct node * NodePointer;

/* Prototype Declaration */
void insert(int);
NodePointer search(int);
void delete(int);
NodePointer getTargetNode(NodePointer);
NodePointer getTargetNodeinCase1(NodePointer);
NodePointer getTargetNodeinCase2(NodePointer);
void printBinaryTree();
void printInorder(NodePointer);
void printPreorder(NodePointer);

/* Global Variable */
NodePointer root;

/* Main */
int main() {
  int i, n, k;
  char command[15];
  NodePointer find, next;

  root = (NodePointer)malloc(sizeof(struct node));
  root = NULL;

  scanf("%d", &n);
  for ( i = 0; i < n; i++ ) {
    scanf("%s", command);
    if ( strcmp(command, "insert") == 0 ) {
      scanf("%d", &k);
      insert(k);
    }else if ( strcmp(command, "find") == 0 ) {
      scanf("%d", &k);
      find = search(k);
      if ( find == NULL ) printf("no\n");
      else printf("yes\n");
    }else if ( strcmp(command, "delete") == 0 ) {
      scanf("%d", &k);
      delete(k);
    }else if ( strcmp(command, "print") == 0 ) {
      printBinaryTree();
    }
  }

  return 0;
}

/* Insert to Binary Tree */
void insert(int keydata) {
  NodePointer x = root;
  NodePointer y = NULL;
  NodePointer z;

  z = (NodePointer)malloc(sizeof(struct node));

  z->key = keydata;
  z->left = NULL;
  z->right = NULL;

  //Set Parent of New Node
  while (x != NULL) {
    y = x;
    if ( z->key < x->key ) x = x->left;
    else x = x->right;
  }
  z->parent = y;

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

/* Search Key from Binary Tree */
NodePointer search(int keydata) {
  NodePointer r = root;

  while ( r != NULL && r->key != keydata) {
    if ( r->key < keydata) r = r->right;
    else r = r->left;
  }

  return r;
}

/* Delete Node from Binary Tree */
void delete(int keydata) {
  NodePointer tmp, del, next;

  // Search for Target Node
  tmp = search(keydata);

  if ( tmp->left == NULL || tmp->right == NULL ) del = tmp;
  else del = getTargetNode(tmp);
  
  if ( del->left != NULL ) next = del->left;
  else next = del->right;

  if ( next != NULL ) next->parent = del->parent;

  if ( del->parent == NULL ) root = next;
  else if ( del == del->parent->left )
    del->parent->left = next;
  else del->parent->right = next;

  if ( del != tmp )  tmp->key = del->key;

  free(del);
}

/* Get Target Node */
NodePointer getTargetNode(NodePointer x) { 
  if ( x->right != NULL )
    return getTargetNodeinCase1(x->right);

  return getTargetNodeinCase2(x->parent);
}

/* Return Target Node in Case 1 */
NodePointer getTargetNodeinCase1(NodePointer x) {
  if ( x->left == NULL ) return x;

  return getTargetNodeinCase1(x->left);
}

/* Return Target Node in Case 2 */
NodePointer getTargetNodeinCase2(NodePointer x) {
  NodePointer y = x;
  
  if ( y == NULL || x == y->left ) return y;

  return getTargetNodeinCase2(y->parent);
}

/* Print Binary Tree */
void printBinaryTree() {
  //Inorder Walk
  printInorder(root);
  printf("\n");
  //Preorder Walk
  printPreorder(root);
  printf("\n");
}

/* Preorder Walk */
void printPreorder(NodePointer u) {
  if ( u == NULL ) return;

  printf(" %d", u->key);
  printPreorder(u->left);
  printPreorder(u->right);
}

/* Inorder Walk */
void printInorder(NodePointer u) {
  if ( u == NULL ) return;

  printInorder(u->left);
  printf(" %d", u->key);
  printInorder(u->right);
}

Measured memory_bytes: 19360