#include <stdio.h>
#include <iostream>
#include <utility>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
typedef long long ll;

struct node
{
    int key;
    node *parent, *left, *right;
};

node *root = nullptr;
void insert(node *z)
{
    node *y = nullptr;
    node *x = root;
    while (x != nullptr)
    {
        y = x;
        if (z->key < x->key)
            x = x->left;
        else
            x = x->right;
    }
    z->parent = y;

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

bool find(int k)
{
    node *x = root;
    while (x != nullptr)
    {
        if (x->key == k)
            return true;
        else if (x->key > k)
            x = x->left;
        else
            x = x->right;
    }
    return false;
}
node *getNextNode(node *x) //return x's succesor
{

    if (x->right == nullptr)
        return x->parent;

    node *y;
    x = x->right;
    while (x != nullptr)
    {
        y = x;
        x = x->left;
    }

    return y;
}
void del (int k)
{
    node *x = root;
    while (x != nullptr)
    {
        if (x->key == k)
            break;
        else if (x->key > k)
            x = x->left;
        else
            x = x->right;
    }

    if (x == nullptr)
        return;

    if (x->left == nullptr && x->right == nullptr)
    {
        if (x->parent == nullptr)
            root = nullptr;
        else
        {
            if (x->parent->left == x)
                x->parent->left = nullptr;
            else
                x->parent->right = nullptr;
        }
    }
    else if (x->left == nullptr)
    {
        if (x->parent == nullptr)
        {
            root = x->right;
            x->right->parent = nullptr;
        }
        else
        {
            if (x->parent->left == x)
            {
                x->parent->left = x->right;
                x->right->parent = x->parent;
            }
            else
            {
                x->parent->right = x->right;
                x->right->parent = x->parent;
            }
        }
    }
    else if (x->right == nullptr)
    {
        if (x->parent == nullptr)
        {
            root = x->left;
            x->left->parent = nullptr;
        }
        else
        {
            if (x->parent->left == x)
            {
                x->parent->left = x->left;
                x->left->parent = x->parent;
            }
            else
            {
                x->parent->right = x->left;
                x->left->parent = x->parent;
            }
        }
    }
    else
    {
        int key = getNextNode(x)->key;
        del(key);
        x->key = key;
    }
}

void Preorder(node *x)
{
    printf(" %d", x->key);
    if (x->left != nullptr)
        Preorder(x->left);
    if (x->right != nullptr)
        Preorder(x->right);
}

void Inorder(node *x)
{
    if (x->left != nullptr)
        Inorder(x->left);
    printf(" %d", x->key);
    if (x->right != nullptr)
        Inorder(x->right);
}

int main()
{
    int m, k;
    string str;
    cin >> m;

    for (int i = 0; i < m; i++)
    {
        cin >> str;
        if (str[0] == 'i')
        {
            node *z = (node *)malloc(sizeof(node));
            cin >> z->key;
            z->left = nullptr;
            z->right = nullptr;
            z->parent = nullptr;
            insert(z);
        }
        else if (str[0] == 'f')
        {
            cin >> k;
            if (find(k))
                printf("yes\n");
            else
                printf("no\n");
        }
        else if(str[0]=='d')
        {
            cin >> k;
            del(k);
        }
        else
        {
            Inorder(root);
            printf("\n");
            Preorder(root);
            printf("\n");
        }
    }
    return 0;
}


Measured memory_bytes: 21996