#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>

using namespace std;
template <class T>
struct node{
    T d;
    pair<node<T>*,node<T>*> ch;
    node(){
        ch.first=nullptr;
        ch.second=nullptr;
    }
};
template <class T>
class tree{
public:
    void insert(const T& num){
        dinsert(num,root);
    }
    bool find(const T& num){
        return dfind(num,root)!=nullptr;
    }
    bool erase(const T& num){
        return derase(num,root);
    }
    void print(){
        stringstream str1,str2;
        dprintf(str1,str2,root);
        cout<<str1.str()<<endl<<str2.str()<<endl;
    }
    tree(){
        root=nullptr;
    }
private:
    node<T>* root;
    void dinsert(const T& num,node<T>*& dleaf){
        if(dleaf==nullptr){
            dleaf=new node<T>;
            dleaf->d=num;
            return;
        }
        dinsert(num,dleaf->d>num?dleaf->ch.first:dleaf->ch.second);
    }
    node<T>* dfind(const T& num,node<T>*& dleaf){
        if(dleaf==nullptr){
            return dleaf;
        }else if(dleaf->d==num){
            return dleaf;
        }
        return dfind(num,dleaf->d>num?dleaf->ch.first:dleaf->ch.second);
    }
    bool derase(const T& num,node<T>*& dleaf){
        if(dleaf->d==num){
            if(dleaf->ch.first==nullptr&&dleaf->ch.second==nullptr){
                node<T>* ddleaf=dleaf;
                dleaf=nullptr;
                delete ddleaf;
                return true;
            }else if(dleaf->ch.first==nullptr||dleaf->ch.second==nullptr){
                if(dleaf->ch.first!=nullptr){
                    node<T>* ddleaf=dleaf;
                    dleaf=dleaf->ch.first;
                    delete ddleaf;
                }else{
                    node<T>* ddleaf=dleaf;
                    dleaf=dleaf->ch.second;
                    delete ddleaf;
                }
            }else{
                node<T>*& ddleaf=dmin(dleaf->ch.second);
                node<T>* dddleaf=ddleaf;
                dleaf->d=ddleaf->d;
                if(dleaf->ch.second!=nullptr){
                    ddleaf=ddleaf->ch.second;
                }
                delete dddleaf;
            }
            return true;
        }else if(dleaf==nullptr){
            return false;
        }
        return derase(num,dleaf->d>num?dleaf->ch.first:dleaf->ch.second);
    }
    node<T>*& dmax(node<T>*& dleaf){
        if(dleaf->ch.second==nullptr){
            return dleaf;
        }
        return dmax(dleaf->ch.second);
    }
    node<T>*& dmin(node<T>*& dleaf){
        if(dleaf->ch.first==nullptr){
            return dleaf;
        }
        return dmin(dleaf->ch.first);
    }
    void dprintf(stringstream& str1,stringstream& str2,node<T>* dleaf){
        str2<<' '<<dleaf->d;
        if(dleaf->ch.first!=nullptr){
            dprintf(str1,str2,dleaf->ch.first);
        }
        str1<<' '<<dleaf->d;
        if(dleaf->ch.second!=nullptr){
            dprintf(str1,str2,dleaf->ch.second);
        }
    }
};

int main()
{
    char instr[8];
    tree<int> T;
    int N,num;
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        scanf("%s",instr);
        if(!strcmp(instr,"insert")){
            scanf("%d",&num);
            T.insert(num);
        }else if(!strcmp(instr,"find")){
            scanf("%d",&num);
            if(T.find(num)){
                printf("yes\n");
            }else{
                printf("no\n");
            }
        }else if(!strcmp(instr,"delete")){
            scanf("%d",&num);
            T.erase(num);
        }else{
            T.print();
        }
    }
}

Measured memory_bytes: 26836