#include <iostream>
#include <cstdio>
#include <cmath>
// #include <stack>
// #include <vector>
#include <cstring>
#include <cstdlib>
// #include <algorithm>
using namespace std;

struct Node {
    int key = 0;
    Node *parent, *left, *right;
};

Node *NIL;

struct Tree {
    Node *root;
};

void insert(Tree *T, int key) {
    Node *y = NIL;
    Node *x = T->root;
    Node *n = (Node *)malloc(sizeof(Node));
    n->key = key;
    n->parent = NIL;
    n->left = NIL;
    n->right = NIL;
    
    while (x != NIL) {
        y = x;
        if (key < x->key) {
            x = x->left;
        } else {
            x = x->right;
        }
    }
    
    n->parent = y;

    if (y == NIL) {
        T->root = n;
    } else {
        if (key < y->key) {
            y->left = n;
        } else {
            y->right = n;
        }
    }
};

Node* find(Node *N, int key) {
    Node *x;
    x = N;
    
    while(x != NIL && x->key != key) {
        if (key < x->key) {
            x = x->left;
        } else {
            x = x->right;
        }
    }
    
    return x;
}

Node* getMin(Node *x) {
    while (x->left != NIL) x = x->left;
    return x;
}

Node* getSuccessor(Node *x) {
    if (x->right != NIL) {
        return getMin(x->right);
    }
    
    Node *y = x->parent;
    while (y != NIL && x == y->right) {
        x = y;
        y = y->parent;
    }
    
    return y;
}

void deleteByKey(Tree *T, int key) {
    Node *z = find(T->root, key);
    if (z == NIL) {
        return;
    }
    Node *y; // to deleted
    Node *x; // child
    
    if (z->left == NIL || z->right == NIL) {
        y = z;
    } else {
        y = getSuccessor(z);
    }
    
    if (y->left != NIL) {
        x = y->left;
    } else {
        x = y->right;
    }

    if (x != NIL) {
        x->parent = y->parent;
    }

    if (y->parent == NIL) {
        T->root = x;
    } else {
        if (y->parent->left == y) {
            y->parent->left = x;
        } else {
            y->parent->right = x;
        }
    }
    
    if (y != z) {
        z->key = y->key;
    }
    
    free(y);
}

void inorder(Node *N) {
    if (N == NIL) {
        return;
    }

    inorder(N->left);
    cout << " " << N->key;
    inorder(N->right);
}

void preorder(Node *N) {
    if (N == NIL) {
        return;
    }
    
    cout << " " << N->key;
    preorder(N->left);
    preorder(N->right);
}

int main() {
    int N;
    scanf("%d", &N);
    Tree *T = (Tree *)malloc(sizeof(Tree));
    T->root = NIL;

    char command[10];
    int key;
    
    for (int i = 0; i < N; i++) {
        scanf("%s %d", command, &key);
        
        if (!strcmp(command, "insert")) {
            insert(T, key);
            continue;
        }
        
        if (!strcmp(command, "find")) {
            Node *x = find(T->root, key);
            if (x != NIL) {
                cout << "yes" << endl;
            } else {
                cout << "no" << endl;
            }
            continue;
        }
        
        if (!strcmp(command, "delete")) {
            deleteByKey(T, key);
            continue;
        }
        
        if (!strcmp(command, "print")) {
            inorder(T->root);
            cout << endl;
            preorder(T->root);
            cout << endl;
            continue;
        }
    }
    
    return 0;
}



Measured memory_bytes: 21892