#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <string>
#include <map>
#include <set>
#define ll long long
#define FAST_IO ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;

ll n, k;
string s;

struct node
{
	ll val;
	node *l, *r;
};

struct tree
{
	node * root;	
};

void insert(tree *t, ll x)
{
	node *p = new node();
	p -> val = x;
	p -> l = NULL;
	p -> r = NULL;
	if(t -> root == NULL) t -> root = p;
	else
	{
		node *temp = t -> root;
		while(temp != NULL)
		{
			if(x < temp -> val)
			{
				if(temp -> l == NULL) 
				{
					temp -> l = p;
					return ;
				} 
				else temp = temp -> l;
			}
			else
			{
				if(temp -> r == NULL) 
				{
					temp -> r = p;
					return ;
				}
				else temp = temp -> r;
			}
		}
	}
}

void find(tree *t, ll x)
{
	node * temp = t -> root;
	ll f = 0;
	if(temp -> val == x)
	{
		cout << "yes" << endl;
		return ;
	}
	while(temp != NULL)
	{
		if(temp -> val == x) 
		{
			cout << "yes" << endl;
			f = 1;
			return ;
		}
		else if(temp -> val > x) temp = temp -> l;
		else if(temp -> val < x) temp = temp -> r;
	}
	if(!f) cout << "no" << endl; 
}

void delet(tree *t, ll x)
{
	node * temp = t -> root;
	node * cnt = NULL;
	while(temp != NULL)
	{
		if(temp -> val == x)
		{
			if(temp -> l == NULL && temp -> r == NULL)
			{
				if(cnt == NULL) 
				{
					temp = NULL;
					t -> root = NULL;
				}
				else if(cnt -> l != NULL && cnt -> l -> val == x) 
				{
					cnt -> l = NULL;
				}
				else if(cnt -> r != NULL && cnt -> r -> val == x) 
				{
					cnt -> r = NULL; 
				}
				return ;
			}
			else if(temp -> l == NULL && temp -> r != NULL)
			{
				if(cnt == NULL)
				{
					t -> root = temp -> r;
				}
				else
				{
					if(cnt -> l != NULL && cnt -> l -> val == x) cnt -> l = temp -> r;
					else if(cnt -> r != NULL && cnt -> r -> val == x) cnt -> r = temp -> r;
				}
				return ;
			}
			else if(temp -> r == NULL && temp -> l != NULL)
			{
				if(cnt == NULL)
				{
					t -> root = temp -> l;
				}
				else
				{
					if(cnt -> l != NULL && cnt -> l -> val == x) cnt -> l = temp -> l;
					else if(cnt -> r != NULL && cnt -> r -> val == x) cnt -> r = temp -> l;
				}
				return ;
			}
			else if(temp -> l != NULL && temp -> r != NULL)
			{
				node *ans = temp;
				temp  = temp -> r;
				if(temp -> l == NULL) 
				{
					ans -> val = temp -> val;
					ans -> r = NULL;
				} 
				else
				{
					while(temp -> l -> l != NULL)
					{
						temp = temp -> l;
					}
					ans -> val = temp -> l -> val;
					temp -> l = NULL;
				}
//				node *ans = temp -> r;
//				if(cnt != NULL && cnt -> l != NULL && cnt -> l -> val == x)
//				{
//				 	cnt -> l = temp -> l;
//				 	temp = temp -> l;
//				 	while(temp != NULL)
//				 	{
//				 		if(temp -> r == NULL) 
//				 		{
//				 			temp -> r = ans;
//				 			return ;
//						}
//				 		else temp = temp -> r;
//					}
//					return ;
//				}         
//				else if(cnt != NULL && cnt -> r != NULL && cnt -> r -> val == x)
//				{
//					cnt -> r = temp -> l;
//					temp = temp -> l;
//					while(temp != NULL)
//					{ 
//				 		if(temp -> r == NULL) 
//				 		{
//				 			temp -> r = ans;
//							return ;
//						}
//				 		else temp = temp -> r;
//					}
//					return ;
//				} 
//				else if(cnt == NULL)
//				{
//					t -> root = temp -> l;
//					temp = temp -> l;
//					while(temp != NULL)
//				 	{
//				 		if(temp -> r == NULL)
//				 		{
//				 			 temp -> r = ans;
//				 			 return ; 
//						}
//				 		else temp = temp -> r;
//					}
//					return ;
//				}                               
			}
		}
		else if(temp -> val > x) 
		{
			cnt = temp;
			temp = temp -> l;
		}
		else if(temp -> val < x) 
		{
			cnt = temp;
			temp = temp -> r;
		}
	}
}
void inorder(node * temp)
{
	if(temp != NULL)
	{
		inorder(temp -> l);
		cout << " " << temp -> val;
		inorder(temp -> r);
	}
}
void preorder(node * temp)
{
	if(temp != NULL)
	{
		cout << " " << temp -> val;
		preorder(temp -> l);
		preorder(temp -> r);
	} 
}

void print(tree *t, ll x)
{
	inorder(t -> root);
	cout << endl;
	preorder(t -> root);
	cout << endl;
}

node *Delet(node *root, ll key)
{
	if(!root) return NULL;
	if(root -> val > key)
	{
		root -> l = Delet(root -> l, key);
	}
	else if(root -> val < key)
	{
		root -> r = Delet(root -> r, key);
	}
	else
	{
		if(!root -> l || !root -> r)
		{
			root = (root -> l) ? root -> l : root -> r;
		}
		else
		{
			node * now = root -> r;
			while(now -> l) now = now -> l;
			root -> val = now -> val;
			root -> r = Delet(root -> r, now -> val);
		}
	}
	return root;
}

tree t;

int main()
{
	cin >> n;
	while(n--)
	{
		cin >> s;
		if(s == "print") print(&t, k);
		else cin >> k;
		if(s == "insert")  insert(&t, k);
		else if(s == "find") find(&t, k);
		else if(s == "delete")
		{
			//(&t) -> root = Delet((&t) -> root, k);
			delet(&t, k);
		}
	}
	return 0;
}
/*30
insert 7
insert 4
insert 5
insert 11
insert 9
insert 14
insert 8
insert 10
insert 13
insert 15*/


Measured memory_bytes: 15556