#include<iostream>
#include<cstring>
using namespace std;

struct Node{//binary tree's node
    int key;
    Node *parent, *left, *right;
};

Node *root, *NUL;

void insert(int node){
    //Now Searching...
    Node *nowNode = root;//start with root
    Node *nowParent = NUL;//root node's parent node is NULL
    while(nowNode!=NUL){
        if(nowNode->key<node){
            nowParent = nowNode;
            nowNode = nowParent -> right;
        }else{
            nowParent = nowNode;
            nowNode = nowNode -> left;
        }
    }
    //Now Creating new Node
    Node *newNode = new Node();
    newNode->parent = nowParent;
    newNode->key = node;
    newNode->left = newNode->right = NUL;
    //Now Inserting
    if(nowParent==NUL){//empty tree's root node
        root = newNode;
    }else if(node<nowParent->key){
        nowParent->left = newNode;
    }else{
        nowParent->right = newNode;
    }
}

void preOrder(Node *nowNode){
    if(nowNode==NUL) return;
    cout<<' '<<nowNode->key;
    preOrder(nowNode->left);
    preOrder(nowNode->right);
}

void inOrder(Node *nowNode){
    if(nowNode==NUL) return;
    inOrder(nowNode->left);
    cout<<' '<<nowNode->key;
    inOrder(nowNode->right);
}

/*void inOrderTest(Node *nowNode){
    if(nowNode==NUL) return;
    inOrderTest(nowNode->left);
    cout<<"\nkey: "<<nowNode->key
        <<"  parent: "
        <<nowNode->parent
        <<"  left-child: "
        <<nowNode->left
        <<"  right-child: "
        <<nowNode->right;
        if(nowNode->parent!=NUL){
            cout<<"  parent: "<<nowNode->parent->key;
        }
        if(nowNode->left!=NUL){
            cout<<"  left: "<<nowNode->left->key;
        }
        if(nowNode->right!=NUL){
            cout<<"  right: "<<nowNode->right->key;
        }
        cout<<endl;
    inOrderTest(nowNode->right);
}*/

bool find(int toFind, Node *nowNode){
    //cout<<"-- now finding :"<<nowNode->key<<endl;
    if(nowNode==NUL) return false;
    if(nowNode->key == toFind) return true;
    if(toFind>nowNode->key){
        return find(toFind, nowNode->right);
    }else{
        return find(toFind, nowNode->left);
    }
}

Node* findDelete(int toFind, Node *nowNode){
    if(nowNode==NUL) return NUL;
    if(nowNode->key == toFind) return nowNode;
    if(toFind>nowNode->key){
        return findDelete(toFind, nowNode->right);
    }else{
        return findDelete(toFind, nowNode->left);
    }
}

void update(Node *toDelete){//update and release
    //cout<<"parent: "<<toDelete->parent->key<<endl;
    //cout<<"toDelete: "<<toDelete->key<<endl;
    if(toDelete->parent->left == toDelete){
        toDelete->parent->left=NUL;
    }else{
        toDelete->parent->right=NUL;
    }
    delete(toDelete);
}

void deleteNode(Node* toDelete){
    if(toDelete->left == NUL&&toDelete->right==NUL){//leaf node
        //cout<<"this is leaf node"<<endl;
        update(toDelete);
    }else if(toDelete->left != NUL&&toDelete->right!=NUL){//two subnodes
        Node *minNode = toDelete->right;
        while(minNode->left!=NUL){//seek min node
            minNode = minNode->left;
        }
        if(minNode->parent == toDelete){
            toDelete->right= NUL;//if parent node is toDelete
        }else{
            minNode->parent->left=NUL;//update
        }
        toDelete->key = minNode->key;//copy node value
        delete(minNode);//release
    }else if(toDelete->left != NUL){
        if(toDelete->parent->left == toDelete){
            toDelete->parent->left=toDelete->left;
            toDelete->left->parent=toDelete->parent;
        }else{
            toDelete->parent->right=toDelete->left;
            toDelete->left->parent=toDelete->parent;
        }
        delete(toDelete);
    }else{
        //cout<<"right not NULL"<<endl;
        //cout<<"toDelete: "<<toDelete->key<<endl;
        //cout<<"parent: "<<toDelete->parent->key<<endl;
        //Node * parent = toDelete->parent;
        //cout<<"parent: "<<parent->key<<endl;
        if(toDelete->parent->left == toDelete){
            toDelete->parent->left=toDelete->right;
            toDelete->right->parent = toDelete->parent;
        }else{
            //cout<<"now updating"<<endl;
            toDelete->parent->right=toDelete->right;
            toDelete->right->parent = toDelete->parent;
            //cout<<"subnode after update: "<<toDelete->parent->right->key<<endl;
        }
        //cout<<"updated"<<endl;
        delete(toDelete);
    }
}

void deleteNode(int toDelete, Node *nowNode){
    Node *node = findDelete(toDelete, root);
    //cout<<"found: "<<node->key<<endl;
    deleteNode(node);
}

int main(){
    root = NUL;//initial root
    int ope;
    cin>>ope;
    for(int i=0; i<ope; i++){
        string op;
        cin>>op;
        if(op=="print"){
            inOrder(root);
            cout<<endl;
            preOrder(root);
            cout<<endl;
            //inOrderTest(root);
        }else if(op=="insert"){
            int toInsert;
            cin>>toInsert;
            insert(toInsert);
        }else if(op=="find"){
            int toFind;
            cin>>toFind;
            if(find(toFind, root)){
                cout<<"yes"<<endl;
            }else{
                cout<<"no"<<endl;
            }
        }else{
            int toDelete;
            cin>>toDelete;
            deleteNode(toDelete, root);
        }
    }
    return 0;
}



Measured memory_bytes: 21824