#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node
{
    int data;
    struct Node *l, *r;
};

struct Node *new_node(int n)
{
    struct Node *ret = (struct Node *)malloc(sizeof(struct Node));
    ret->data = n;
    ret->l = NULL;
    ret->r = NULL;
    return ret;
}

void insert_node(struct Node **root, int val)
{
    if ((*root) == NULL)
    {
        *(root) = new_node(val);
        return;
    }
    else
    {
        if ((*root)->data == val)
        {
            return;
        }
        if ((*root)->data > val)
        {
            insert_node(&((*root)->l), val);
        }
        else
        {
            insert_node(&((*root)->r), val);
        }
    }
}

struct Node *del_min(struct Node **root)
{
    struct Node *p, *pp = NULL;
    p = *root;
    while (p->l != NULL)
    {
        pp = p;
        p = p->l;
    }
    if (pp == NULL)
    {
        *root = (*root)->r;
    }
    else
    {
        pp->l = p->r;
    }
    return p;
}
struct Node *del_node(struct Node **root, int val)
{
    struct Node *p, *q;
    if (*root == NULL)
    {
        return NULL;
    }
    if ((*root)->data == val)
    {
        q = *root;
        if ((*root)->l == NULL)
        {
            (*root) = (*root)->r;
        }
        else
        {
            if ((*root)->r == NULL)
            {
                (*root) = (*root)->l;
            }
            else
            {
                p = del_min(&((*root)->r));
                p->l = (*root)->l;
                p->r = (*root)->r;
                (*root) = p;
            }
        }
        return q;
    }
    else
    {
        if ((*root)->data > val)
        {
            return del_node(&((*root)->l), val);
        }
        else
        {
            return del_node(&((*root)->r), val);
        }
    }
}

void print_inorder(struct Node *root)
{
    if (root != NULL)
    {
        print_inorder(root->l);
        printf(" %d", root->data);
        print_inorder(root->r);
    }
}

void print_preorder(struct Node *root)
{
    if (root != NULL)
    {
        printf(" %d", root->data);
        print_preorder(root->l);
        print_preorder(root->r);
    }
}
void print(struct Node *root)
{
    print_inorder(root);
    printf("\n");
    print_preorder(root);
    printf("\n");
}
struct Node *find_node(struct Node *root, int val)
{
    if (root == NULL)
    {
        return NULL;
    }
    if (root->data == val)
    {
        return root;
    }
    else
    {
        if (root->data > val)
        {
            return find_node(root->l, val);
        }
        else
        {
            return find_node(root->r, val);
        }
    }
}

int main()
{
    struct Node *root = NULL;
    int q, i;
    char s[64];
    scanf("%d", &q);
    for (i = 0; i < q; i++)
    {
        scanf("%s ", s);
        if (strcmp(s, "insert") == 0)
        {
            int t;
            scanf("%d ", &t);
            insert_node(&root, t);
        }
        else if (strcmp(s, "find") == 0)
        {
            int t;
            scanf("%d ", &t);
            if (find_node(root, t) != NULL)
            {
                printf("yes\n");
            }
            else
            {
                printf("no\n");
            }
        }
        else if (strcmp(s, "delete") == 0)
        {
            int t;
            scanf("%d ", &t);
            del_node(&root, t);
        }
        else if (strcmp(s, "print") == 0)
        {
            print(root);
        }
    }
    return 0;
}
