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

typedef struct node *link;
struct node{
    int key;
    link par, left, right; //???????????????????????????
};

link head = NULL;

//root????????¨????????¨???z????????\??????
void insert(link root, link z){
    link y = NULL;
    link x = head;
    
    while(x!=NULL){
        y = x;
        if(z->key < x->key){
            x = x->left;
        }else{
            x = x->right;
        }
    }
    z->par = y; //???????¨????
    
    if(y==NULL){
        head = z;
    }else if(z->key < y->key){
        y->left = z;
    }else{
        y->right = z;
    }
    return;
}

//???????????????
void printInorder(link node){
    if(node==NULL) return;
    printInorder(node->left);
    printf(" %d",node->key);
    printInorder(node->right);
}

//???????????????
void printPreorder(link node){
    if(node==NULL) return;
    printf(" %d",node->key);
    printPreorder(node->left);
    printPreorder(node->right);
}

int findKey(link node,int value){
    if(node==NULL) return 0;
    
    if(findKey(node->left,value)) return 1;
    if(node->key == value) return 1;
    if(node->key > value) return 0;
    
    return findKey(node->right,value);
}

//k????¬??????????key??¨??????????????????????????????
link findNextNode(link node, int k){
    if(node == NULL) return NULL;
    
    link res = findNextNode(node->left, k);
    if(res != NULL)
    if(res->key > k) return res;
    
    if(node != NULL)
    if(node->key > k) return node;
    
    return findNextNode(node->right, k);
}

//??¨??????key?????????value???????????????????????????
void deleteKey(link node, int value){
    link z = node;
    int k = 0;
    
    //value??¨????????????key???????????????????????¢???
    while(z != NULL){
        k = z->key;
        if(k == value) break;
        
        if(k < value){
            z = z->right;
        }else{
            z = z->left;
        }
    }
    
    //????????????????????????????????£?????´???
    if(z == NULL){
        printf("node not found\n");
        return;
    }
    
    //??????????????????
    link next = NULL;
    if(z->left != NULL && z->right != NULL){ //z?????????2???????????´???
        next = findNextNode(node, z->key); //y????¬??????????????????¢???
        
        z->key = next->key; //y???key?????´??°
        
        value = next->key; //????????????????????´??°
        z = next; //??????????????????????????´??°
        
    }
    
    
    //??????????????£????????????????????????????????????(z???p??????????????´??????????????????????????°??§????????????)
    link p = z->par; //z??????
    if(z->left == NULL && z->right == NULL){ //z???????????????????????´???
        if(p->left != NULL){ //p?????????z????????£???????????´???
            if(p->left->key == value){
                p->left = NULL;
            }else{
                p->right = NULL;
            }
        }else{
            p->right = NULL;
        }
    }else if(z->left != NULL){ //z???????????????????????´???
        if(p->left != NULL){ //p????????????????????£???????????´???
            if(p->left->key == value){
                p->left = z->left;
                z->left->par = p;
            }else{
                p->right = z->left;
                z->left->par = p;
            }
        }else{
            p->right = z->left;
            z->left->par = p;
        }
    }else{ //z???????????????????????´???
        if(p->left != NULL){ //p????????????????????£???????????´???
            if(p->left->key == value){
                p->left = z->right;
                z->right->par = p;
            }else{
                p->right = z->right;
                z->right->par = p;
            }
        }else{
            p->right = z->right;
            z->right->par = p;
        }
    }
    free(z);
    
    return;
}

int main(){
    int n,i,j,k;
    
    scanf("%d",&n);
    
    int value=0;
    char com[100];
    for(i=0;i<n;i++){
        scanf("%s",com);
        
        if(!strcmp(com,"insert")){
            scanf("%d",&value);
            
            link z;
            z = malloc(sizeof(struct node));
            z->key = value;
            z->left = z->right = NULL;
            
            insert(head,z);
            
        }else if(!strcmp(com,"find")){
            scanf("%d",&value);
            
            if(findKey(head,value)){
                printf("yes\n");
            }else{
                printf("no\n");
            }
            
        }else if(!strcmp(com,"delete")){
            scanf("%d",&value);
            deleteKey(head,value);
            
        }else if(!strcmp(com,"print")){
            printInorder(head);
            printf("\n");
            printPreorder(head);
            printf("\n");
        }
    }
    
    return 0;
}