#include<stdio.h>
#include<stdlib.h>
struct tree {
        int data;
        struct tree *left;
        struct tree *right;
};
struct tree *root = NULL;
int Search(struct tree*, int);
struct tree *Insert(struct tree*, int);
struct tree *Alsearch(struct tree**);
struct tree *Remove(struct tree**, int);
void Dispm(struct tree*);
void Dispp(struct tree*);
main()
{
        int i, count, key;
        char command[7];
        scanf("%d", &count);
        for (i = 0; i < count; i++) {
                scanf("%*c%s", command);
                switch (command[0]) {
                case 'f':
                        scanf(" %d", &key);
                        if (Search(root, key)) {
                                printf("yes\n");
                        } else {
                                printf("no\n");
                        }
                        break;
                case 'i':
                        scanf(" %d", &key);
                        root = Insert(root, key);
                        break;
                case 'd':
                        scanf(" %d", &key);
                        Remove(&root, key);
                        break;
                case 'p':
                        Dispm(root);
                        putchar('\n');
                        Dispp(root);
                        putchar('\n');
                        break;
                }
        }
}
int Search(struct tree *p, int key)
{
        if (p == NULL) {
                return 0;
        } else if (p -> data == key) {
                return 1;
        } else if (p -> data > key) {
                return Search(p -> left, key);
        } else {
                return Search(p -> right, key);
        }
}
struct tree *Insert(struct tree *p, int key)
{
        if (p == NULL) {
                p = (struct tree*)malloc(sizeof(struct tree));
                p -> data = key;
                p -> left = NULL;
                p -> right = NULL;
        } else if (p -> data > key) {
                p -> left = Insert(p -> left, key);
        } else {
                p -> right = Insert(p -> right, key);
        }
        return p;
}
struct tree *Alsearch(struct tree **p)
{
        if ((*p) -> left == NULL) {
                struct tree *tmp = *p;
                *p = (*p) -> right;
                return tmp;
        } else {
                return Alsearch(&(*p) -> left);
        }
}

struct tree *Remove(struct tree **p, int key)
{
        if (*p == NULL) {
                return;
        } else if ((*p) -> data > key) {
                        (*p) -> left = Remove(&(*p) -> left, key);
                } else if ((*p) -> data < key) {
                        (*p) -> right = Remove(&(*p) -> right, key);
    } else {
                struct tree *tmp = *p;
                if ((*p) -> right == NULL) {
                                        *p = (*p) -> left;
                        free(tmp);
                } else if ((*p) -> left == NULL) {
                                        *p = (*p) -> right;
                        free(tmp);
                                } else {
                                        struct tree *alter = Alsearch(&(*p) -> right);
                                        alter -> left = (*p) -> left;
                                        alter -> right = (*p) -> right;
                        *p = alter;
                        free(tmp);
                }
        }
        return *p;
}
void Dispm(struct tree *p)
{
        if (p != NULL) {
                Dispm(p -> left);
                printf(" %d", p -> data);
                Dispm(p -> right);
        }
}
void Dispp(struct tree *p)
{
    if (p != NULL) {
        printf(" %d", p -> data);
        Dispp(p -> left);
        Dispp(p -> right);
    }
}