#include<bits/stdc++.h>

using namespace std;

struct node
{
	int num,fa,ls,rs;
	node(int _num,int _fa)
	{
		num=_num;
		fa=_fa;
		ls=rs=-1;
	}
};

typedef vector<node> Tree;

Tree T;

void insert(int t,int n)
{
	if(T[0].num==INT_MAX) 
		T[0].num=n;
	else if(T[t].num>n)
		if(T[t].ls==-1)
		{
			T[t].ls=T.size();
			T.push_back(node(n,t));
		}
		else insert(T[t].ls,n);
	else
		if(T[t].rs==-1)
		{
			T[t].rs=T.size();
			T.push_back(node(n,t));
		}
		else insert(T[t].rs,n);
}

void find(int t,int x)
{
	if(T.size()==0||t==-1)
		printf("no\n");
	else if(T[t].num==x)
		printf("yes\n");
	else if(x<T[t].num)
		find(T[t].ls,x);
	else find(T[t].rs,x);
}

void lc(int fa,int son)
{
	T[fa].ls=son;
	if(son!=-1) T[son].fa=fa; 
}

void rc(int fa,int son)
{
	T[fa].rs=son;
	if(son!=-1) T[son].fa=fa;
}

void del(int t,int x)
{
	if(t==-1) return ;
	if(T[t].num==x)
	{
		if(T[t].ls==-1)
			if(T[t].rs==-1)
				if(T[T[t].fa].ls==t)
					T[T[t].fa].ls=-1;
				else T[T[t].fa].rs=-1;
			else
				if(T[T[t].fa].ls==t)
				{
					T[T[t].fa].ls=T[t].rs;
					T[T[t].rs].fa=T[t].fa;
				}
				else
				{
					T[T[t].fa].rs=T[t].rs;
					T[T[t].rs].fa=T[t].fa;
				}
		else if(T[t].rs==-1)
				if(T[T[t].fa].ls==t)
				{
					T[T[t].fa].ls=T[t].ls;
					T[T[t].ls].fa=T[t].fa;
				}
				else
				{
					T[T[t].fa].rs=T[t].ls;
					T[T[t].ls].fa=T[t].fa;
				}
			else
			{
				int mir=T[t].rs;
				while(T[mir].ls!=-1) mir=T[mir].ls;
				del(t,T[mir].num);
				T[t].num=T[mir].num;
			}
	}
	else if(x<T[t].num)
		del(T[t].ls,x);
	else del(T[t].rs,x);
}

void Preorder(int t)
{
	if(t==-1) return;
	printf(" %d",T[t].num);
	Preorder(T[t].ls);
	Preorder(T[t].rs);
}

void Inorder(int t)
{
	if(t==-1) return;
	Inorder(T[t].ls);
	printf(" %d",T[t].num);
	Inorder(T[t].rs);
}

void print()
{
	Inorder(T[1].ls);
	printf("\n");
	Preorder(T[1].ls);
	printf("\n");
}

int main()
{
	T.clear();
	T.push_back(node(INT_MAX,1));
	T.push_back(node(-1,-1));
	T[1].ls=0;
	int n;
	scanf("%d",&n);
	while(n--)
	{
		char s[10];
		scanf("%s",s);
		if(s[0]=='i')
		{
			int t;
			scanf("%d",&t);
			insert(T[1].ls,t);
		}
		else if(s[0]=='f')
		{
			int t;
			scanf("%d",&t);
			find(T[1].ls,t);
		}
		else if(s[0]=='d')
		{
			int t;
			scanf("%d",&t);
			del(T[1].ls,t);
		}
		else print();
	}
	return 0;
}
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <numeric>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <list>
#include <stack>
#include <cctype>
#include <cmath>

using namespace std;

/* typedef */

typedef long long ll;

/* constant */
const int INF = 1 << 30;
const int MAX = 500000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
struct Node {
    Node *parent, 
         *left, 
         *right;
    int key;
};

Node *root, *NIL;

/* function */
void insert(int value);
Node* findNode(int target);
Node* getSuccessor(Node* target);
Node* getMinimum(Node* target);
void deleteNode(Node* target);
void preorder(Node* v);
void inorder(Node* v);

/* main */
int main(){

    // main
    int n, val;
    cin >> n;
    string man;
    for (int i = 0; i < n; i++) {
        cin >> man;
        if (man == "print") {
            inorder(root); cout << '\n';
            preorder(root); cout << '\n';
        }
        else {
            cin >> val;
            if (man == "insert") {
                insert(val);
            }
            else if (man == "delete" ) {
                Node* target = findNode(val);
                deleteNode(target);
            }
            else {
                Node* ret_node = findNode(val);
                if (ret_node != NIL)
                    cout << "yes" << '\n';
                else
                    cout << "no" << '\n';
            }
        }
    }

}

void deleteNode(Node* target) {
    Node* y;  // delete Node
    Node* x;  // y's child

    // determin delete Node : y;
    if (target->left == NIL || target->right == NIL)
        y = target;
    else
        y = getSuccessor(target);

    // determin y's child : x;
    if (y->left != NIL) x = y->left;
    else x = y->right;

    if (x != NIL) 
        x->parent = y->parent;

    if (y->parent == NIL) 
        root = x;
    else if (y == y->parent->left) 
        y->parent->left = x;
    else
        y->parent->right = x;

    if (y != target)
        target->key = y->key;
}

Node* getSuccessor(Node* target) {
    // target has right subtree
    if (target->right != NIL)
        return getMinimum(target->right);

    /*
    Node* y = target->parent;
    while (y != NIL && target == y->right) {
        target = y;
        y = y->parent;
    }
    return y;
    */
}

Node* getMinimum(Node* target) {
    while (target->left != NIL) 
        target = target->left;

    return target;
}

Node* findNode(int target) {

    Node* x = root;
    while (x != NIL) {
        if (target == x->key) {
            return x;
        }
        else if (target < x->key) {
            x = x->left;
        }
        else {
            x = x->right;
        }
    }
    return x;
}

void insert(int value) {

    // init Node
    Node* z = new Node();
    z->parent = NIL;
    z->left = NIL;
    z-> right = NIL;

    Node* y = NIL;  // parent of x(root)
    Node* x = root; // root of 'T'

    while (x != NIL) {
        y = x; // set parent
        if (value < x->key) {
            x = x->left;
        }
        else {
            x = x->right;
        }
    }

    z->parent = y;
    z->key = value;

    if (y == NIL) {  // 'T' is empty
        root = z;
    }
    else if (value < y->key) {
        y->left = z;  // z is y's left
    }
    else if (value > y->key) {
        y->right = z;  // z is y's right
    }
}

void preorder(Node* v) {
    if (v != NIL) {
        cout << ' ' << v->key;
        preorder(v->left);
        preorder(v->right);
    }

}

void inorder(Node* v) {
    if (v != NIL) {
        inorder(v->left);
        cout << ' ' << v->key;
        inorder(v->right);
    }
}

