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

template <typename T>
class BinaryTree{
  private:
    struct TreeNode{
      T value;
      TreeNode* left, *right, *parent;
    };

    struct Tree{
      TreeNode* root;
      TreeNode* nil;
    };

    Tree self;

    TreeNode* makeNode(T v, TreeNode* l, TreeNode* r, TreeNode* p){
      TreeNode* newNode = new TreeNode;
      newNode->left = l;
      newNode->right = r;
      newNode->parent = p;
      newNode->value = v;
      return newNode;
    }

  public:
    BinaryTree(){
      self.nil = makeNode(0, nullptr, nullptr, nullptr);
      self.root = self.nil;
    }

    TreeNode* newNode(T v){
      return makeNode(v, self.nil, self.nil, self.nil);
    }

    TreeNode* getMin(TreeNode* node){
        while (node->left != self.nil)
          node = node->left;
        return node;
    }

    void insert(T value){
      TreeNode* z = newNode(value);
      TreeNode* y = self.nil;
      TreeNode* x = self.root;
      while ( x != self.nil ){
        y = x;
        if ( z->value < x->value )
          x = x->left;
        else
          x = x->right;
      }
      z->parent = y;


      if ( y == self.nil )
        self.root = z;
      else {
          if ( z->value < y->value )
            y->left = z;
        else
            y->right = z;
      }
    }

    TreeNode* getSuccessor(TreeNode* node){
      if (node->right != self.nil)
        return getMin(node->right);
        
      TreeNode* y = node->parent;
      while ( y!= self.nil && node == y->right){
          node = y;
          y = y->parent;
      }
      return y;
    }

    void del(T value){
      TreeNode* node = find(value);
      if (node == nullptr) return;
      this->del(node);
    }

    void del(TreeNode* node){
      TreeNode* y;
      if (node->right == self.nil || node->left == self.nil)
        y = node;
      else
        y = getSuccessor(node);

      TreeNode* x;
      if ( y->left != self.nil )
        x = y->left;
      else
        x = y->right;

      if ( x != self.nil )
        x->parent = y->parent;

      if ( y->parent == self.nil )
        self.root = x;
      else if ( y == y->parent->left )
        y->parent->left = x;
      else
        y->parent->right = x;

      if ( y != node )
        node->value = y->value;
    }

    TreeNode* find(T value){
      return find(value, self.root);
    }

    TreeNode* find(T value, TreeNode* node){
      if (node == self.nil)
        return nullptr;
      if (node->value == value) return node;
      if (value < node->value)
        return find(value, node->left);
      else
        return find(value, node->right);
    }

    void printInorder(TreeNode* root){
      if (root == self.nil) return;
      printInorder(root->left);
      printf(" %d", root->value);
      printInorder(root->right);
    }

    void printPreorder(TreeNode* root){
      if (root == self.nil) return;
      printf(" %d", root->value);
      printPreorder(root->left);
      printPreorder(root->right);
    }

    void print(void){
      printInorder(self.root);
      printf("\n");
      printPreorder(self.root);
      printf("\n");
    }
};

int main(){
  BinaryTree<int> bTree;
  string str;
  int n;
  cin >> n;
  for (int i=0;i<n;i++){
    cin >> str;
    if (str == "insert"){
      int value;
      cin >> value;
      bTree.insert(value);
    }
    else if (str == "find"){
      int value;
      cin >> value;
      cout << ((bTree.find(value)!=nullptr)?"yes":"no") << endl;
    }
    else if(str == "delete"){
      int value;
      cin >> value;
      bTree.del(value);
    }
    else if (str == "print")
      bTree.print();
  }
  return 0;
}





Measured memory_bytes: 22020