#include<iostream>
using namespace std;
#include<stdio.h>
#include<string>

typedef struct fox{ int val; fox *left, *right; } fox_n;

fox_n* init()
{
    fox_n* root = new fox_n;
    root->val = -2000000001;
    root->left = NULL;
    root->right = NULL;
    return root;
}

void insert(fox_n* root, int key)
{
    static bool first = true;
    static bool add_leaf = true;  // true:left, false:right.
    if(first){ first = false; root->val = key; return; }  // initialize.
    fox_n* canis;
    fox_n* water = root;  // water from root to leaf.
    do{
        canis = water;
        if(key < canis->val){  // canis chases water.
            water = canis->left;
            add_leaf = true;
        }else{
            water = canis->right;
            add_leaf = false;
        }
    }while(water != NULL);

    fox_n* leaf = new fox_n;  // add new leaf.
    leaf->val = key;
    leaf->left = NULL;
    leaf->right = NULL;
    if(add_leaf){ canis->left = leaf; }else{ canis->right = leaf; }
    return;
}

bool find(fox_n* root, int key)
{
    fox_n* fox = root;
    int val;
    do{
        val = fox->val;
        if(key < val){ fox = fox->left; }
        else if(val < key){ fox = fox->right; }
        else{ return true; }
    }while(fox);
    return false;
}

void inorder(fox_n* root)
{
    if(!root) return;
    inorder(root->left);
    printf(" %d", root->val);
    inorder(root->right);
    return;
}

void preorder(fox_n* root)
{
    if(!root) return;
    printf(" %d", root->val);
    preorder(root->left);
    preorder(root->right);
    return;
}

void delete_key_act1(fox_n* fox)
{
    // no parent, or have two children.
    fox_n *wolf, *lycaon;
    if(fox->right != NULL){
        wolf = fox->right;
        lycaon = fox;
        while(wolf->left){ lycaon = wolf; wolf = wolf->left; };
        fox->val = wolf->val;
        if(lycaon == fox){ lycaon->right = NULL; }else{ lycaon->left = NULL; }
        delete wolf;
    }else if(fox->left != NULL){
        wolf = fox->left;
        lycaon = fox;
        while(wolf->right){ lycaon = wolf; wolf = wolf->right; };
        fox->val = wolf->val;
        if(lycaon == fox){ lycaon->left = NULL; }else{ lycaon->right = NULL; }
        delete wolf;
    }
    return;
}

void delete_key_act2(fox_n* fox, fox_n* canis)
{
    // other. 
    fox_n* child;
    child = (fox->left != NULL ? fox->left : fox->right);
    (canis->left == fox ? canis->left : canis->right) = child;
    delete fox;
    return;
}

void delete_key(fox_n* root, int key)
{
    if(root->left == NULL && root->right == NULL) return;
    fox_n* canis = root;
    fox_n* fox = root;
    while(fox){
        if(fox->val == key){
            if(fox == root || (fox->left != NULL && fox->right != NULL)){ delete_key_act1(fox); }
            else{ delete_key_act2(fox, canis); }
            return;
        }
        canis = fox;
        if(key < fox->val){ fox = fox->left; }
        else{ fox = fox->right; }
    };
    return;
}

int main()
{
    fox_n* root = init();
    int i, n, key; char cmd[6]; //string cmd;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        //cin >> cmd;
        scanf("%s", cmd);
        if(cmd[0] == 'i'){ scanf("%d", &key); insert(root, key); }
        else if(cmd[0] == 'f'){ scanf("%d", &key); (find(root, key) ? printf("yes\n") : printf("no\n")); }
        else if(cmd[0] == 'p'){ inorder(root); printf("\n"); preorder(root); printf("\n"); }
        else{ scanf("%d", &key); delete_key(root, key);
        }
    }
    return 0;
}

Measured memory_bytes: 13704