#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NIL -1

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

struct Node *root = NULL;

void freeNode(struct Node *target) {
    if (target == NULL) return;
    if (target->left != NULL) freeNode(target->left);
    if (target->right != NULL) freeNode(target->right);
    free(target);
}

void deleteNode(int key, struct Node *target) {
    // printf("key is %d. target->key is %d\n", key, target->key);

    if (key == target->key) {

        /// 子がない
        if (target->left == NULL && target->right == NULL) {
            // printf("not children\n");
            if (target->parent->left != NULL) {
                if (key == target->parent->left->key) target->parent->left = NULL;
            }
            if (target->parent->right != NULL) {
                if (key == target->parent->right->key) target->parent->right = NULL;
            }
            free(target);
            return;
        }

        /// 子が左だけ
        if (target->left != NULL) {
            // printf("exist left child\n");
            if (target->parent != NULL) {
                if (target->parent->left != NULL) {
                    if (key == target->parent->left->key) {
                        target->parent->left = target->left;
                        target->left->parent = target->parent;
                    }
                }
                if (target->parent->right != NULL) {
                    if (key == target->parent->right->key) {
                        target->parent->right = target->left;
                        target->left->parent = target->parent;
                    }
                }

            } else {
                target->left->parent = NULL;
                root = target->left;
            }

            free(target);
            return;
        }

        /// 子が右だけ
        if (target->right != NULL) {
            // printf("exist right child\n");
            if (target->parent != NULL) {
                // printf("target->parent != NULL\n");
                if (target->parent->left != NULL) {
                    // printf("target->parent->left != NULL\n");
                    if (key == target->parent->left->key) {
                        // printf("key == target->parent->left->key\n");
                        target->parent->left = target->right;
                        target->right->parent = target->parent;
                    }
                }
                if (target->parent->right != NULL) {
                    // printf("target->parent->right != NULL\n");
                    if (key == target->parent->right->key) {
                        // printf("key == target->parent->right->key\n");
                        target->parent->right = target->right;
                        target->right->parent = target->parent;
                    }
                }

            } else {
                target->right->parent = NULL;
                root = target->right;
            }

            free(target);
            return;
        }

        /// 子が2つ
        if (target->left != NULL && target->right != NULL) {
            // printf("exist children\n");
            int tmp = target->key;
            target->key = target->right->key;
            target->right->key = tmp;
            deleteNode(key, target->right);
        }

    } else if (key < target->key) {
        // printf("deleteNode(key, target->left)\n");
        deleteNode(key, target->left);

    } else {
        // printf("deleteNode(key, target->right)\n");
        deleteNode(key, target->right);
    }
}

int findNode(int key, struct Node *target) {
    if (target == NULL) return 0;

    if (key == target->key) {
        return 1;

    } else if (key < target->key) {
        return findNode(key, target->left);

    } else {
        return findNode(key, target->right);
    }
}

void insertNode(struct Node *insertTarget, struct Node *insertedTarget) {
    if (insertedTarget->key > insertTarget->key) {
        if (insertedTarget->left == NULL) {
            insertedTarget->left = insertTarget;
            insertTarget->parent = insertedTarget;
        } else {
            insertNode(insertTarget, insertedTarget->left);
        }

    } else {
        if (insertedTarget->right == NULL) {
            insertedTarget->right = insertTarget;
            insertTarget->parent = insertedTarget;
        } else {
            insertNode(insertTarget, insertedTarget->right);
        }
    }
}

void insertTree(struct Node *target) {
    if (root == NULL) root = target;
    else insertNode(target, root);
}

void printInOrderTree(struct Node *target) {
    if (target->left != NULL) printInOrderTree(target->left);
    printf(" %d", target->key);
    if (target->right != NULL) printInOrderTree(target->right);
}

void printPreOrderTree(struct Node *target) {
    printf(" %d", target->key);
    if (target->left != NULL) printPreOrderTree(target->left);
    if (target->right != NULL) printPreOrderTree(target->right);
}

struct Node* createNode(int key) {
    struct Node *node = (struct Node *)malloc(sizeof(struct Node));
    node->key = key;
    node->parent = NULL;
    node->left = NULL;
    node->right = NULL;
    return node;
}

int main() {
    int n;
    scanf("%d", &n);

    char command[10];
    int key;
    for (int i = 0; i < n; i++) {
        scanf("%s", command);

        if (command[0] == 'i') {
            scanf("%d", &key);
            insertTree(createNode(key));

        } else if (command[0] == 'f') {
            scanf("%d", &key);
            if (findNode(key, root)) printf("yes\n");
            else printf("no\n");

        } else if (command[0] == 'd') {
            scanf("%d", &key);
            deleteNode(key, root);

        } else {
            printInOrderTree(root);
            printf("\n");
            printPreOrderTree(root);
            printf("\n");
        }
    }

    // freeNode(root);

    return 0;
}

