#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_a_genius = 0; ngtkana_is_a_genius < int(n); ngtkana_is_a_genius++)
#define rep(i, begin, end) for (int i = int(begin); (i) < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto& a, auto b){ if (a > b) {a = b; return true;} return false; };
auto cmx = [](auto& a, auto b){ if (a < b) {a = b; return true;} return false; };
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) { std::cerr << " " << head; debug_impl(tail...); }
#ifndef STOPIT
#define debug(...)\
  do {\
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
    debug_impl(__VA_ARGS__);\
    std::cerr << std::noboolalpha;\
  } while (false)
#else
#define debug(...) 0;
#endif

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  struct node_type {
    node_type * left;
    node_type * right;
    std::int32_t value;
  };
  node_type * top = nullptr;
  auto insert = [&] (std::int32_t x) {
    if (!top) {
      top = ::new node_type{ nullptr, nullptr, x };
    } else {
      node_type * node = top;
      while (true) {
        node_type * & next = (x <= node->value ? node->left : node->right);
        if (next) node = next;
        else {
          next = ::new node_type{ nullptr, nullptr, x };
          break;
        }
      }
    }
  };

  auto find = [&] (std::int32_t x) {
    assert(top);
    node_type * node = top;
    while (true) {
      if (x == node->value) return true;
      node_type * next = (x < node->value ? node->left : node->right);
      if (!next) return false;
      node = next;
    }
  };

  auto erase = [&] (std::int32_t x) {
    node_type * parent = nullptr;
    node_type * node = top;
    assert(node);
    while (true) {
      if (x == node->value) break;
      parent = std::exchange(node, (x <= node->value ? node->left : node->right));
      assert(node);
    }
    assert(!parent || parent->left == node || parent->right == node);
    assert(node->value == x);
    node_type * & node_ref = (parent->left == node ? parent->left : parent->right);
    if (!node->left && !node->right) {
      node_ref = nullptr;
    } else if (!node->left) {
      node_ref = node->right;
    } else if (!node->right) {
      node_ref = node->left;
    } else {
      if (!node->right->left) {
        node->right->left = node->left;
        node_ref = node->right;
      } else {
        node_type * parent_of_next = nullptr;
        node_type * next = node->right;
        while (next->left) {
          parent_of_next = std::exchange(next, next->left);
        }
        assert(!parent_of_next || parent_of_next->left == next);
        node_type * & next_ref = parent_of_next->left;
        next_ref = next->right;
        node_ref = next;
        node_ref->left = node->left;
        node_ref->right = node->right;
      }
    }
    delete node;
  };

  auto print = [&] {
    std::vector< int > pre;
    std::vector< int > mid;
    auto dfs = [&](auto&& dfs, node_type * now) -> void {
      pre.emplace_back(now->value);
      if (now->left) dfs(dfs, now->left);
      mid.emplace_back(now->value);
      if (now->right) dfs(dfs, now->right);
    };
    dfs(dfs, top);
    for (int x : mid) std::cout << " " << x;
    std::cout << std::endl;
    for (int x : pre) std::cout << " " << x;
    std::cout << std::endl;
  };

  int n; std::cin >> n;
  loop(n){
    std::string s; std::cin >> s;
    switch (s.front()) {
      case 'i' : {
        int x; std::cin >> x;
        insert(x);
        break;
      }
      case 'f' : {
        int x; std::cin >> x;
        bool ans = find(x);
        std::cout << (ans ? "yes" : "no") << std::endl;
        break;
      }
      case 'd' : {
        int x; std::cin >> x;
        erase(x);
        break;
      }
      case 'p' : {
        print();
        break;
      }
    }
  }
  return 0;
}


Measured memory_bytes: 18844