#include <iostream>
#include <vector>
#include <string>
#include <limits>

using namespace std;

int count,minimum;

void pre(vector<vector<int> > &tree,int now)
{
	cout << " " << tree[now][0];
	for(int i=1;i<3;i++)
	{
		if(tree[now][i] != minimum)
		{
			pre(tree,tree[now][i]);
		}
	}
}

void ino(vector<vector<int> > &tree,int now)
{
	if(tree[now][1] == minimum && tree[now][2] == minimum)
	{
		cout << " " << tree[now][0];
	}
	else
	{
		if(tree[now][1]!=minimum)
		{
			ino(tree,tree[now][1]);
		}
		cout << " " << tree[now][0];
		if(tree[now][2]!=minimum)
		{
			ino(tree,tree[now][2]);
		}
	}
}

void print(vector<vector<int> > &tree)
{
	ino(tree,0);
	cout << endl;
	pre(tree,0);
	cout << endl;
}

void insert(vector<vector<int> > &tree,int key,int now)
{
	if(count == 0)					//root none
	{
		tree[0][0] = key;
		tree[0][3] = -1;
	}
	else
	{
		if(tree[now][0] > key)		//left
		{
			if(tree[now][1] == minimum)	//child none
			{
				tree[now][1] = count;
				tree[count][0] = key;
				tree[count][3] = now;
			}
			else											//child exist
			{
				insert(tree,key,tree[now][1]);
			}
		}
		else						//right
		{
			if(tree[now][2] == minimum)	//child none
			{
				tree[now][2] = count;
				tree[count][0] = key;
				tree[count][3] = now;
			}
			else
			{
				insert(tree,key,tree[now][2]);
			}
		}
	}
}

bool find(vector<vector<int> > &tree, int key,int now)
{
	bool jud;
	if(tree[now][0]==key)
	{
		return true;
	}
	if(tree[now][0] > key)
	{
		if(tree[now][1] == minimum)
		{
			return false;
		}
		else
		{
			jud = find(tree,key,tree[now][1]);
		}
	}
	else
	{
		if(tree[now][2] == minimum)
		{
			return false;
		}
		else
		{
			jud = find(tree,key,tree[now][2]);
		}
	}
	return jud;
}

int findPosition(vector<vector<int> > &tree, int key,int now)
{
	int position=-1;
	if(tree[now][0]==key)
	{
		return now;
	}
	if(tree[now][0] > key)
	{
		if(tree[now][1] == minimum)
		{
			return -1;
		}
		else
		{
			position = findPosition(tree,key,tree[now][1]);
		}
	}
	else
	{
		if(tree[now][2] == minimum)
		{
			return -1;
		}
		else
		{
			position = findPosition(tree,key,tree[now][2]);
		}
	}
	return position;
}

int next(vector<vector<int> > &tree,int start)
{
	int ans = -1;
	if(tree[start][1] == minimum)
	{
		return start;
	}
	else
	{
		ans = next(tree,tree[start][1]);
	}
	return ans;
}

void delete_node(vector<vector<int> > &tree, int key, int start)
{
	int position = findPosition(tree,key,start);
	int parent = tree[position][3];

	if(tree[position][1]==minimum && tree[position][2]==minimum)	//no child
	{
		if(tree[parent][0] > key)
		{
			tree[parent][1] = minimum;
		}
		else
		{
			tree[parent][2] = minimum;
		}
	}
	else if(tree[position][1]!=minimum && tree[position][2]!=minimum) 	//two children
	{
		int next_node = next(tree,tree[position][2]);
		tree[position][0] = tree[next_node][0];
		delete_node(tree,tree[position][0],tree[position][2]);
	}
	else if(tree[position][1]!=minimum)	//one child left
	{
		if(tree[parent][0] > key)
		{
			tree[parent][1] = tree[position][1];
			tree[tree[position][1]][3] = parent;
		}
		else
		{
			tree[parent][2] = tree[position][1];
			tree[tree[position][1]][3] = parent;
		}
	}
	else	//one child right
	{
		if(tree[parent][0] > key)
		{
			tree[parent][1] = tree[position][2];
			tree[tree[position][2]][3] = parent;
		}
		else
		{
			tree[parent][2] = tree[position][2];
			tree[tree[position][2]][3] = parent;
		}
	}
}

int main(void)
{
	count = 0;
	minimum = numeric_limits<int>::min();
    vector<vector<int> > tree;
	int n,num;
	string command;
	cin >> n;
    tree = vector<vector<int> >(n,vector<int>(4,minimum));

	for(int i=0;i<n;i++)
	{
		cin >> command;
		if(command == "insert")
		{
			cin >> num;		//input node num
			insert(tree,num,0);
			count++;
		}
		else if(command == "print")
		{
			print(tree);
		}
		else if(command == "find")
		{
			cin >> num;
			if(find(tree,num,0))
			{
				cout << "yes" << endl;
			}
			else
			{
				cout << "no" << endl;
			}
		}
		else if(command == "delete")
		{
			cin >> num;
			delete_node(tree,num,0);
		}
	}
	return 0;
}

Measured memory_bytes: 57432