#include <cassert>
#include <cstdlib>
#include <iostream>

class Node {
  public:
    Node* parent;
    Node* left;
    Node* right;
    int val;
    Node(int);
};

Node::Node(int _val)
{
  parent = nullptr;
  left = nullptr;
  right = nullptr;
  val = _val;
}

Node* root = nullptr;

void do_insert(int operand)
{
  Node* node = root;
  Node* prev = nullptr;
  while (node != nullptr) {
    prev = node;
    if (operand < node->val) {
      node = node->left;
    } else {
      node = node->right;
    }
  }
  Node* new_node = new Node(operand);
  if (prev == nullptr) {
    new_node->parent = root;
    root = new_node;
  } else {
    new_node->parent = prev;
    if (operand < prev->val) {
      prev->left = new_node;
    } else {
      prev->right = new_node;
    }
  }
}

Node* do_find(int operand)
{
  Node* node = root;
  while (node != nullptr) {
    if (node->val == operand) {
      return node;
    } else if (operand < node->val) {
      node = node->left;
    } else {
      node = node->right;
    }
  }
  return nullptr;
}

Node* find_largest_node(Node* subroot)
{
  Node* node = subroot;
  while (node->right) {
    node = node->right;
  }
  return node;
}

Node* find_smallest_node(Node* subroot)
{
  Node* node = subroot;
  while (node->left) {
    node = node->left;
  }
  return node;
}

void do_delete(int operand)
{
  Node* victim = do_find(operand);
  if (victim == nullptr) {
    return;
  }
  // victim has less than two children
  if (victim->left == nullptr || victim->right == nullptr) {
    Node* child = victim->left == nullptr ? victim->right : victim->left;
    if (victim->parent->left == victim) {
      victim->parent->left = child;
    } else {
      victim->parent->right = child;
    }
    if (child) {
      child->parent = victim->parent;
    }
    delete victim;
  } else { // victim has two children
    Node* successor = find_smallest_node(victim->right);
    assert(successor->left == nullptr);
    if (successor->parent->left == successor) { // give the child of the successor
      successor->parent->left = successor->left;
    } else {
      successor->parent->right = successor->right;
    }
    victim->val = successor->val;
    delete successor;
  }
}

void print_inorder(Node* node)
{
  if (node == nullptr) {
    return;
  }
  print_inorder(node->left);
  std::cout << ' ' << node->val;
  print_inorder(node->right);
}

void print_preorder(Node* node)
{
  if (node == nullptr) {
    return;
  }
  std::cout << ' ' << node->val;
  print_preorder(node->left);
  print_preorder(node->right);
}

void do_print()
{
  print_inorder(root);
  std::cout << std::endl;
  print_preorder(root);
  std::cout << std::endl;
}

int main()
{
  int noperations;
  std::cin >> noperations;
  for (int i = 0; i < noperations; i++) {
    int operand;
    std::string operation;
    std::cin >> operation;
    if (operation == "insert") {
      std::cin >> operand;
      do_insert(operand);
    } else if (operation == "find") {
      std::cin >> operand;
      if (do_find(operand)) {
        std::cout << "yes" << std::endl;
      } else {
        std::cout << "no" << std::endl;
      }
    } else if (operation == "delete") {
      std::cin >> operand;
      do_delete(operand);
    } else if (operation == "print") {
      do_print();
    } else {
      std::cerr << "Unexpected operation found: " << operation << std::endl;
      exit(1);
    }
  }
  return 0;
}



Measured memory_bytes: 21936