#include <iostream>
#include <string>
using namespace std;

struct Node {
  int key;
  Node *parent, *left, *right;
};

Node *root, *NIL;

void insert(int key) {
  Node *y = NIL;
  Node *x = root;
  Node *z = new Node();
  z->key = key;

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

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

void deleteNode(Node *node) {
  Node *target, *child;
  if (node->left == NIL || node->right == NIL) {
    target = node;
  } else {
    target = node->right;
    while (1) {
      if (target->left == NIL) {
        break;
      }
      target = target->left;
    }
  }

  if (target->left != NIL) {
    child = target->left;
  } else {
    child = target->right;
  }
  if (child != NIL) {
    child->parent = target->parent;
  }

  if (target->parent == NIL) {
    root = child;
  } else if (target == target->parent->left) {
    target->parent->left = child;
  } else {
    target->parent->right = child;
  }

  if (target != node) {
    node->key = target->key;
  }

  delete target;
}

Node* find(Node *node, int key) {
  while (node != NIL && key != node->key) {
    if (key < node->key) {
      node = node->left;
    } else {
      node = node->right;
    }
  }
  return node;
}

void printPreorder(Node *node) {
  cout << " " << node->key;
  if (node->left != NIL) printPreorder(node->left);
  if (node->right != NIL) printPreorder(node->right);
}

void printInorder(Node *node) {
  if (node->left != NIL) printInorder(node->left);
  cout << " " << node->key;
  if (node->right != NIL) printInorder(node->right);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  string command;
  int v;
  for (int i = 0; i < n; i++) {
    cin >> command;
    if (command == "insert") {
      cin >> v;
      insert(v);
    } else if (command == "find") {
      cin >> v;
      if(find(root, v) == NIL) {
        cout << "no" << endl;
      } else {
        cout << "yes" << endl;
      }
    } else if (command == "delete") {
      cin >> v;
      deleteNode(find(root, v));
    } else if (command  == "print") {
      printInorder(root);
      cout << endl;
      printPreorder(root);
      cout << endl;
    }
  }

  return 0;
}