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

typedef struct node
{
    int key;
    struct node *left,*right;
}Node;

Node *root,*NIL;

void insert(int key)
{
    Node *y=NIL;
    Node *x=root;

    Node *z=(Node *)malloc(sizeof(Node));
    z->key=key;
    z->left=NIL;
    z->right=NIL;

    while(x!=NIL)
    {
        y=x;
        if(key< x->key)
            x=x->left;
        else
            x=x->right;
    }

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

int find(int key)
{
    Node *x=root;
    while(x!=NIL)
    {
        if(key == x->key)return 1;
        else if(key < x->key)x=x->left;
        else x=x->right;
    }
    return 0;
}

void del(int key)
{
    if(!find(key))return;

    Node *y=NIL;
    Node *x=root;
    while(x!=NIL &&key !=x->key)
    {
        y=x;
        if(key<x->key)
            x=x->left;
        else
            x=x->right;
    }
    printf("---------%d    %d------\n",y->key,x->key);
    if(x->left==NIL && x->right==NIL)
    {
        if(y->left ==x)
            y->left=NIL;
        else
            y->right=NIL;
        free(x);
    }
    else if(y->left == x)
    {
        if(x->left==NIL && x->right != NIL)
        {
            y->left=x->right;
            free(x);
        }
        else if(x->right==NIL && x->left != NIL)
        {
            y->left=x->left;
            free(x);
        }
        else
        {
            Node *z=x->left;
            while(z->right!= NIL)z=z->right;
            z->right=x->right;
            y->left=x->left;
            free(x);
        }
    }
    else if(y->right == x)
    {
        if(x->left==NIL && x->right != NIL)
        {
            y->right=x->right;
            free(x);
        }
        else if(x->right==NIL && x->left != NIL)
        {
            y->right=x->left;
            free(x);
        }
        else
        {
            Node *z=x->left;
            while(z->right!= NIL)z=z->right;
            z->right=x->right;
            y->right=x->left;
            free(x);
        }

    }

}

void inorder(Node *t)
{
    if(t)
    {
        inorder(t->left);
        printf(" %d",t->key);
        inorder(t->right);
    }
}

void preorder(Node *t)
{
    if(t)
    {
        printf(" %d",t->key);
        preorder(t->left);
        preorder(t->right);
    }
}

int main()
{
    int n;
    char com[20];
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%s",com);
        if(com[0]=='i')
        {
            int key;
            scanf("%d",&key);
            insert(key);
        }
        else if(com[0]=='f')
        {
            int key;
            scanf("%d",&key);
            if(find(key))
                printf("yes\n");
            else
                printf("no\n");
        }
        else if(com[0]=='d')
        {
            int key;
            scanf("%d",&key);
            del(key);
        }
        else if(com[0]=='p')
        {
            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
        }
    }
}

