#include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
//#include <unordered_map> //???????????\??¢??°
#include <fstream> //ifstream, ofstream

#define NDEBUG //NDEBUG???#include <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert


using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???                           ????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#define din cin
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout dummy.str(""); dummy.clear(stringstream::goodbit); dummy //dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUTFILE????????????????????¬???????????????????????????????????¢????????????
//#define OUTPUTFILE "output.txt" //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif

//?¨??????\???????????????????????????????????????????????´??????INPUTFROMTEXTFILE????????????????????¬???????????????????????????????????¢????????????
//#define INPUTFILE "input.txt" //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif


#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A) dout << setw(3) << (A) << " " // << setw(3) ??????????????\????????????
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)

#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef unsigned long long ll;

const int INF = 1e9-1;



class Node {
public:
    Node(int value) {
        v = value;
        p = NULL;
        l = NULL;
        r = NULL;
    }
    
    
    int v;
    Node* p;
    Node* l;
    Node* r;
};



class BinaryTree {
public:
    BinaryTree() {
        root = NULL;
    }
    
    
    Node* root;
    
    
    void insert(int value) {
        Node* x = new Node(value); //????????????????????????
        
        
        Node* ptr = root; //tree??°?????¨???????????????
        Node* ptr_parent = NULL;
        while( ptr!=NULL ) { //NULL????????????????????§?????????????????¨???ptr_parent????????????????????????
            ptr_parent = ptr;
            if( x->v < ptr->v ) ptr = ptr->l;
            else ptr = ptr->r;
        }
        
        
        if(ptr_parent==NULL) { //tree???????????£?????????????????¨????????????????????????
            root = x;
            return;
        }
        else if( x->v < ptr_parent->v) {
            ptr_parent->l = x;
            x->p = ptr_parent;
        }
        else {
            ptr_parent->r = x;
            x->p = ptr_parent;
        }
    }
    
    void deletE(int key) {
//        dout << "==================\n";
//        dout << "deletE( " << key << " )\n";
        
        Node* ptr = root; //tree??°?????¨???????????????
        while( ptr!=NULL ) {
            if( key == ptr->v ) break;
            else if( key < ptr->v ) ptr = ptr->l;
            else ptr = ptr->r;
        }
        
        assert(ptr!=NULL); //??¶????????????????????????????????????????????¨?????????
        
        recursive_deleteE(ptr);
    }
    
    
    void recursive_deleteE(Node* ptr) { //?????????ptr???????????????
//        dout<<"------\n";
//        dout<<"recursive_deleteE( "<< ptr->v <<" )\n";
        
//        disp(1);
        
        if(ptr->p==NULL) { //??????????±?????????????????????°?????????????±????root??§????????¨???????????¨????????§???tree???????????????
            this->root = NULL;
            return;
        }
        
//        disp(2);
        
        //??????ptr????????????????????????????????\??????
        Node** childOfParent;
        //??????????????????->v???????????¨??????NULL->v??¨??????????????????????????????NULL?????§?????????????????????
        if( ptr->p->l != NULL && ptr->p->l->v == ptr->v ) childOfParent = &(ptr->p->l);
        else childOfParent = &(ptr->p->r);
        
        //??????(*childOfParent)?????\?????§??´??°??????
        
//        disp(3);
        
        //ptr????????????3????????????
        if(ptr->l==NULL && ptr->r==NULL) { //??????????±??????????ptr??????????????????????????§????????°???????´???????????????±?????´??°
//            dout<<"both NULL\n";
            (*childOfParent) = NULL;
            delete ptr;
        }
        else if(ptr->r==NULL) { //??????????±??????????ptr????????????1??????????????????????????§????????°???????????????1????????????????????????ptr???????????????
//            dout<<"right NULL\n";
            (*childOfParent) = ptr->l;
            ptr->l->p = ptr->p;
            delete ptr;
        }
        else if(ptr->l==NULL) { //??????????±??????????ptr????????????1??????????????????????????§????????°???????????????1????????????????????????ptr???????????????
//            dout<<"left NULL\n";
            (*childOfParent) = ptr->r;
            ptr->r->p = ptr->p;
            delete ptr;
        }
        else { //??????????±??????????ptr???????????????2?????¨????????£??????????????§????????°???
//            dout<<"has both child\n";
            
            //ptr?????????subtree???????°?????????? (inorder walk??§ptr????¬?????????????) ?????¢???
            Node* min = ptr->r;
            while(min->l!=NULL) {
                min = min->l;
            }
            assert(min!=NULL); //ptr????????????????????£??????????????§NULL??????????????¨??????????????????
            
            (*childOfParent)->v = min->v; //??????????°????????????????ptr?????????????????????
            
            recursive_deleteE(min); //??????????°?????????????????????????
        }
    }
    
    
    
    
    bool find(int key) {
        Node* ptr = root; //tree??°?????¨???????????????
        while( ptr!=NULL ) {
            if( key == ptr->v ) return true;
            else if( key < ptr->v ) ptr = ptr->l;
            else ptr = ptr->r;
        }
        
        return false;
    }
    
    
    static void printInorder(Node* x) {
        if(x==NULL) return;
        
        printInorder(x->l);
                cout << " " << x->v;
//        cout << setw(3) << x->v << " : " << setw(3) << (x->l==NULL?-1:x->l->v) << ", " << setw(3) << (x->r==NULL?-1:x->r->v)<< " [" << setw(3) << (x->p==NULL?-1:x->p->v) << "]" << endl;
        printInorder(x->r);
    }
    
    
    static void printPreorder(Node* x) {
        if(x==NULL) return;
        
                cout << " " << x->v;
//        cout << setw(3) << x->v << " : " << setw(3) << (x->l==NULL?-1:x->l->v) << ", " << setw(3) << (x->r==NULL?-1:x->r->v)<< " [" << setw(3) << (x->p==NULL?-1:x->p->v) << "]" << endl;
        printPreorder(x->l);
        printPreorder(x->r);
    }
    
    static void printPostorder(Node* x) {
        if(x==NULL) return;
        
        printPostorder(x->l);
        printPostorder(x->r);
//                cout << " " << x->v;
        cout << setw(3) << x->v << " : " << setw(3) << (x->l==NULL?-1:x->l->v) << ", " << setw(3) << (x->r==NULL?-1:x->r->v)<< " [" << setw(3) << (x->p==NULL?-1:x->p->v) << "]" << endl;
    }
    
    
};


int main(){
    
    
    BinaryTree tree;
    
    int numOfQuery; din >> numOfQuery;
    string command;
    int x;
    rep(i,0,numOfQuery) {
        din >> command;
        if(command[0]=='i') {
            din >> x;
            tree.insert(x);
        }
        else if(command[0]=='d') {
            din >> x;
            tree.deletE(x);
        }
        else if(command[0]=='f') {
            din >> x;
            cout << (tree.find(x) ? "yes" : "no") << endl;
        } else {
//            dout << "In-order---------\n";
            BinaryTree::printInorder(tree.root);
            cout << endl;
            
//            dout << "Pre-order--------\n";
            BinaryTree::printPreorder(tree.root);
            cout << endl;
            
//            dout << "Post-order--------\n";
//            BinaryTree::printPostorder(tree.root);
//            cout << endl;
        }
    }
    

#ifdef INPUTFILE
    inputfile.close();
#endif
    
#ifdef OUTPUTFILE
    outputfile.close();
    cout << "\"" << OutputFilePath << "\"" << endl;
#endif
    
    return 0;
}

Measured memory_bytes: 21892