#include<iostream>
using namespace std;

class Node
{
    public:
        int key;
        Node* left;
        Node* right;
        Node* parent;
        Node( int k = 0 );
};

Node::Node( int k )
{
    key = k;
    parent = NULL;
    left = NULL;
    right = NULL;
}

class Tree
{
    private:
        Node* root;
    public:
        Tree();
        void Insert( Tree& t, int k );
        void PrintInorder( Tree t, Node* target );
        void PrintPreorder( Tree t, Node* target );
        Node* Find( Tree t, int k );
        void Delete( Tree t, Node* n );
};

Tree::Tree()
{
    root = NULL;
}

void Tree::Insert( Tree& t, int k )
{
    Node* y = NULL;
    Node* x = t.root;

    while( x != NULL )
    {
        y = x;
        if( k < x -> key )
            x = x -> left;
        else
            x = x -> right;
    }

    Node* n = new Node(k);
    n -> parent = y;

    if( y == NULL )
        t.root = n;
    else if( n -> key < y -> key )
        y -> left = n;
    else
        y -> right = n;
}

void Tree::PrintInorder( Tree t, Node* target )
{
    if( target == NULL )
        target = t.root;

    if( target -> left != NULL )
        PrintInorder( t, target -> left );
    
    cout << " " << target -> key;

    if( target -> right != NULL )
        PrintInorder( t, target -> right );
}

void Tree::PrintPreorder( Tree t, Node* target )
{
    if( target == NULL )
        target = t.root;

    cout << " " << target -> key;

    if( target -> left != NULL )
        PrintPreorder( t, target -> left );

    if( target -> right != NULL )
        PrintPreorder( t, target -> right );
}

Node* Tree::Find( Tree t, int k )
{
    Node* target = t.root;

    while( target != NULL )
    {
        if( target -> key < k )
            target = target -> right;
        else if( target -> key > k )
            target = target -> left;
        else
            return target;
    }
    return NULL;
}

void Tree::Delete( Tree t, Node* n )
{
    Node* np = n -> parent;
    if( n -> left == NULL && n -> right == NULL )
    {
        if( np -> left == n )
            np -> left = NULL;
        else
            np -> right = NULL;
        delete n;
    }
    else if( n -> left == NULL )
    {
        if( np -> left == n )
            np -> left = n -> right;
        else
            np -> right = n -> right;
    
        n -> right -> parent = np;
        delete n;
    }
    else if( n -> right == NULL )
    {
        if( np -> left == n )
            np -> left = n -> left;
        else
            np -> right = n -> left;
    
        n -> left -> parent = np;
        delete n;     
    }
    else
    {
        Node* target = n;
        target = target -> right;
        while( target -> left != NULL )
            target = target -> left;
        n -> key = target -> key;
        Delete( t, target );
    }
}

int main()
{
    int n, k;
    string s;
    Tree tree;

    cin >> n;

    for( int i = 0; i < n; i++ )
    {
        cin >> s;
        if( s == "insert" )
        {
            cin >> k;
            tree.Insert( tree, k );
        }
        else if( s == "find" )
        {
            cin >> k;
            if( tree.Find( tree, k ) != NULL )
                cout << "yes" << endl;
            else
                cout << "no" << endl;
        }
        else if( s == "delete" )
        {
            cin >> k;
            tree.Delete( tree, tree.Find( tree, k ) );
        }
        else
        {
            tree.PrintInorder( tree, NULL );
            cout << endl;
            tree.PrintPreorder( tree, NULL );
            cout << endl;
        }
    }
    return 0;
}


Measured memory_bytes: 21916