#include <stdio.h>
 
typedef struct _tree_t {
    int id;
    struct _tree_t *p, *l, *r;
} tree_t;
 
tree_t* tree_insert(tree_t* root, int id){
    tree_t *y = NULL;
    tree_t *x = root;
    tree_t *z = (tree_t*)malloc(sizeof(tree_t));
    z->id = id;
    z->l = z->r = z->p = NULL;
 
    while(x != NULL) {
        y = x;
        if(z->id < x->id) {
            x = x->l;
        } else { 
            x = x->r;
        }
    }
    z->p = y;
    if(y == NULL) {
        root = z;
    } else if(z->id < y->id) {
        y->l = z;
    } else { 
        y->r = z;
    }
     
    return root;
}
 
void print_find(tree_t *tree, int id) {
    if(tree==NULL) {
        printf("no\n");
        return;
    }
     
    if(id == tree->id) {
        printf("yes\n");
        return;
    } else if(id < tree->id) {
        print_find(tree->l, id);
    } else {
        print_find(tree->r, id);
    }
}
 
tree_t* find(tree_t *tree, int id) {
    if(tree==NULL) {
        return NULL;
    }
     
    if(id == tree->id) {
        return tree;
    } else if(id < tree->id) {
        return find(tree->l, id);
    } else {
        return find(tree->r, id);
    }
}
 
/*??????x????????¨????????¨?????¨????????§????°??????????????????????????????????*/
tree_t* tree_minimum(tree_t* x) {
    while(x->l != NULL) {
        x = x->l;
    }
    return x;
}
 
/*????????¢?´¢??¨?????????x????¬??????????????????´?????????????¬????????????????*/
/*??????????????¨????????§x????????§????????????????????´??????NIL?????????*/
tree_t* tree_successor(tree_t* x) {
    tree_t* y;
    if(x->r != NULL) {
        return tree_minimum(x->r);
    }
    y = x->p;
     
    while(y != NULL && x == y->r) {
        x = y;
        y = y->p;
    }
    return y;
}
 
tree_t* tree_delete(tree_t* T, tree_t* z) {
    tree_t* y;
    tree_t* x;
    if(z==NULL) return T;
     
    /* ??????????????????y????±?????????? */
    if(z->l == NULL || z->r == NULL) { 
        y = z; /* z??????????????§??????????????´????????\????????????z */
    } else {
        y = tree_successor(z); /* z ?????????2???????????´??????z????¬??????? */
    }
 
    /*x???y???NIL??§?????????????????????y???????????????????????´??????NIL????¨???? */
    if(y->l != NULL) {
        x = y->l;
    } else {
        x = y->r;
    }
 
    /*y????????¨x???????????????????????´???y???????????????*/
    /*x???NIL?????????y???????????¨???????¢?????????¶?????????*/
    if(x != NULL) {
        x->p = y->p; /*z???????????????????????´???*/
    }
    if(y->p == NULL) {
        T = x;
    } else if(y == y->p->l) {
        y->p->l = x;
    } else {
        y->p->r = x;
    }
 
    /* z????¬???????????????????????????¨??????y????????????z????§??????????*/
    if(y != z) {
        z->id = y->id;
    }
     
    return T;
}
 
void print_preorder(tree_t *tree) {
    printf(" %d", tree->id);
    if(tree->l != NULL) print_preorder(tree->l);
    if(tree->r != NULL) print_preorder(tree->r);
}
 
void print_inorder(tree_t *tree) {
    if(tree->r == NULL && tree->l == NULL) {
        printf(" %d", tree->id);
    } else if(tree->r == NULL) {
        print_inorder(tree->l);
        printf(" %d", tree->id);
    } else if(tree->l == NULL) {
        printf(" %d", tree->id);
        print_inorder(tree->r);
    } else {
        print_inorder(tree->l);
        printf(" %d", tree->id);
        print_inorder(tree->r);
    }
}
 
int main(void) {
    int i, n, id;
    char buff[100];
    tree_t *root = NULL;
    tree_t *z = NULL;
    scanf("%d\n", &n);
    for(i=0;i<n;i++) {
        scanf("%s %d\n", buff, &id);
        if(buff[0] == 'i') {
            root = tree_insert(root, id);
        } else if(buff[0] == 'f') {
            print_find(root, id);
        } else if(buff[0] == 'd') {
            z = find(root, id);
            root = tree_delete(root, z);
        } else if(buff[0] == 'p') {
            print_inorder(root);
            printf("\n");
            print_preorder(root);
            printf("\n");
        }
    }
     
    return 0;
}

Measured memory_bytes: 19368