#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>

//????????¢?´¢??¨?????§????????????????????????????????§??????????????????????????????????°??????????

using namespace std;

typedef struct s {
    int Key;
    s *Parent;
    s *Left;
    s *Right;
} Node;

Node *root,*NIL;

void insert(int Key){
    Node *x = root;//?§????????????¢?????¬??????root???????????£??±???????????¨?????????
    Node *y = NIL;//??¢?????¬???????????????????????¨????????????NIL??§?????????
    Node *z;//??¬???
    
    z = (Node *)malloc(sizeof(Node));
    z -> Key = Key;
    z -> Left = NIL;
    z -> Right = NIL;
    
    //x???NIL????????????
    while (x != NIL) {
        y = x;
        if (z -> Key < x -> Key) {
            x = x -> Left;
        }else{
            x = x -> Right;
        }
    }
    
    z -> Parent = y;
    //root??????NIL??????????????£?????´???
    //
    if (y == NIL) {
        root = z;
    }else{
        if (z -> Key < y -> Key) {
            y -> Left = z;
        }else{
            y -> Right = z;
        }
    }
}

bool find(int Key){
    Node *f = root;
    
    while (f != NIL) {
        if(f -> Key > Key) f = f -> Left;
        else if (f -> Key < Key) f = f -> Right;
        else return true;
    }
    return false;
}

Node* find_ret(int Key){
    Node *f = root;
    
    while (f != NIL) {
        if(f -> Key > Key) f = f -> Left;
        else if (f -> Key < Key) f = f -> Right;
        else return f;
    }
    return NIL;
}

void inoder(Node *n){
    if (n == NIL) return;
    inoder(n -> Left);
    cout << " " << n -> Key;
    inoder(n -> Right);
}

void preoder(Node *n){
    if (n == NIL) return;
    cout << " " << n -> Key;
    preoder(n -> Left);
    preoder(n -> Right);
}

Node* getNext(Node *k){
    Node *g = k -> Right;
    while (g -> Left != NIL) {
        g = g -> Left;
    }
    return g;
}

void deleteNode(Node *k){
    //k?????????????????°??§??´?????????
    if (k == NIL) {
        return;
    }
    if (k -> Left == NIL && k -> Right == NIL) {
        if (k -> Parent -> Left == k) {//k?????????
            k -> Parent -> Left = NIL;
        }else{//k?????????
            k -> Parent -> Right = NIL;
        }
        free(k);
    }else if(k -> Left != NIL && k -> Right != NIL){
        Node *n = getNext(k);
        k -> Key = n -> Key;
        deleteNode(n);
    }else{
        if (k -> Parent == NIL) {//k???root
            if (k -> Left != NIL) {
                root = k -> Left;
            }else{
                root = k -> Right;
            }
        }else{//k???root????????????
            if (k -> Parent -> Left == k) {//k?????????
                if (k -> Left != NIL) {//k????????????
                    k -> Parent -> Left = k -> Left;
                    k -> Left -> Parent = k -> Parent;
                }else{
                    k -> Parent -> Left = k -> Right;
                    k -> Right -> Parent = k -> Parent;
                }
            }else{//k?????????
                if (k -> Left != NIL) {//k????????????
                    k -> Parent -> Right = k -> Left;
                    k -> Left -> Parent = k -> Parent;
                }else{
                    k -> Parent -> Right = k -> Right;
                    k -> Right -> Parent = k -> Parent;
                }
            }
        }
        free(k);
    }
}

int main(){
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        string com;
        cin >> com;
        if (com == "insert") {
            int num;
            scanf("%d",&num);
            insert(num);
        }else if (com == "find"){
            int num;
            scanf("%d",&num);
            if(find(num)) cout << "yes" << endl;
            else cout << "no" << endl;
        }else if(com == "delete"){
            int num;
            scanf("%d",&num);
            deleteNode(find_ret(num));
        }else{
            inoder(root);
            cout << endl;
            preoder(root);
            cout << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 20016