#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);
}

void findKey(link node,int value){
    link x = node;
    
    while(x!=NULL){
        if(x->key == value){
            printf("yes\n");
            return;
        }
        
        if(x->key > value){
            x = x->left;
        }else{
            x = x->right;
        }
    }
    
    printf("no\n");
    return;
}

//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;
    int dir = -1; //?????????????????????????????´????????¨??????z????????£????????????????¨??????????????????????1????????????0
    
    //value??¨????????????key???????????????????????¢???
    while(z != NULL){
        k = z->key;
        if(k == value) break;
        
        if(k < value){
            z = z->right;
            dir = 0;
        }else{
            z = z->left;
            dir = 1;
        }
    }
    
    //????????????????????????????????£?????´???
    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; //??????????????????????????´??°
        
        if(z->par->left->key == z->key){ //dir??????????¨????
            dir = 1;
        }else{
            dir = 0;
        }
    }
    
    link p = z->par; //z??????
    if(z->left == NULL && z->right == NULL){ //z???????????????????????´???
        if(dir){ //p?????????z???????????´???
            p->left = NULL;
        }else{
            p->right = NULL;
        }
    }else if(z->left != NULL){ //z???????????????????????´???
        if(dir){ //p?????????z???????????´???
            p->left = z->left;
            z->left->par = p;
        }else{
            p->right = z->left;
            z->left->par = p;
        }
    }else{ //z???????????????????????´???
        if(dir){ //p?????????z???????????´???
            p->left = 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);
            findKey(head,value);
            
        }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;
}

Measured memory_bytes: 19364