//#define		FILE_INPUT_DEFINE
#ifdef		FILE_INPUT_DEFINE
#include	<fstream>
#endif		// FILE_INPUT_DEFINE

#include	<iostream>
#include	<deque>
#include	<string>
#include	<algorithm>
#include	<cstdio>
#include	<memory>
#include	<utility>

using namespace std;

enum class Walk
{
	ePreOrder = 0,
	eInOrder,
	ePostOrder
};


struct Node
{
	shared_ptr<Node>		parent;
	shared_ptr<Node>		left;
	shared_ptr<Node>		right;
	int						key;

	Node(int k)
	{
		key = k;
	}
};

shared_ptr<Node>			root;

void insert(int key)
{
	unique_ptr<Node>		z(new Node(key));
	weak_ptr<Node>			x = root;
	weak_ptr<Node>			y;

	while (x.use_count() != 0)
	{
		y = x;
		if (z->key < x.lock()->key)
		{
			x = x.lock()->left;
		}
		else
		{
			x = x.lock()->right;
		}
	}

	if (y.use_count() != 0)
	{
		z->parent = y.lock();
	}

	if (y.use_count() == 0)
	{
		root = move(z);
	}
	else
	{
		if (z->key < y.lock()->key)
		{
			y.lock()->left = move(z);
		}
		else
		{
			y.lock()->right = move(z);
		}
	}
}

void Order(weak_ptr<Node> nd, const Walk &wk)
{
	if (wk == Walk::ePreOrder)
		cout << " " << nd.lock()->key;
	if (nd.lock()->left.use_count() != 0)
		Order(nd.lock()->left, wk);
	if (wk == Walk::eInOrder)
		cout << " " << nd.lock()->key;
	if (nd.lock()->right.use_count() != 0)
		Order(nd.lock()->right, wk);
	if (wk == Walk::ePostOrder)
		cout << " " << nd.lock()->key;
}

auto	find(int key)	->	pair<bool, weak_ptr<Node>>
{
	weak_ptr<Node> nd = root;
	
	while (nd.use_count() != 0)
	{
		if (key == nd.lock()->key)
		{
			return make_pair(true, nd);
		}
		else if (key > nd.lock()->key)
		{
			nd = nd.lock()->right;
		}
		else
		{
			nd = nd.lock()->left;
		}
	}

	return make_pair(false, nd);
}

void del(int key)
{
	pair<bool, weak_ptr<Node>>	Fin(find(key));
	weak_ptr<Node>		nd;

	if (Fin.first)
	{
		nd = Fin.second;
	}
	else
	{
		return;
	}

	if ((nd.lock()->left.use_count() == 0) &&
		(nd.lock()->right.use_count() == 0))
	{
		weak_ptr<Node>		pa;
		pa = nd.lock()->parent;

		if (pa.lock()->left.use_count() == 0)
		{
			pa.lock()->right.reset();
		}
		else
		{
			if (pa.lock()->left->key == nd.lock()->key)
			{
				pa.lock()->left.reset();
			}
			else
			{
				pa.lock()->right.reset();
			}
		}
	}
	else if (nd.lock()->left.use_count() == 0)
	{
		weak_ptr<Node>		pa;
		pa = nd.lock()->parent;

		if (pa.lock()->left.use_count() == 0)
		{
			pa.lock()->right = nd.lock()->right;
			pa.lock()->right->parent = pa.lock();
		}
		else
		{
			if (pa.lock()->left->key == nd.lock()->key)
			{
				pa.lock()->left = nd.lock()->right;
				pa.lock()->left->parent = pa.lock();
			}
			else
			{
				pa.lock()->right = nd.lock()->right;
				pa.lock()->right->parent = pa.lock();
			}
		}
	}
	else if (nd.lock()->right.use_count() == 0)
	{
		weak_ptr<Node>		pa;
		pa = nd.lock()->parent;

		if (pa.lock()->left.use_count() == 0)
		{
			pa.lock()->right = nd.lock()->left;
			pa.lock()->right->parent = pa.lock();
		}
		else
		{
			if (pa.lock()->left->key == nd.lock()->key)
			{
				pa.lock()->left = nd.lock()->left;
				pa.lock()->left->parent = pa.lock();
			}
			else
			{
				pa.lock()->right = nd.lock()->left;
				pa.lock()->right->parent = pa.lock();
			}
		}
	}
	else
	{
		weak_ptr<Node>		pa;
		pa = nd.lock()->parent;

		weak_ptr<Node>		next = nd.lock()->right;
		bool	isNext = true;		//???

		while (next.lock()->left.use_count() != 0)
		{
			isNext = false;			//???
			next = next.lock()->left;
		}
		weak_ptr<Node>		left = nd.lock()->left;
		weak_ptr<Node>		right = nd.lock()->right;
		if (isNext)
		{
			if (pa.lock()->left.use_count() == 0)
			{
				pa.lock()->right = next.lock();
				pa.lock()->right->parent = pa.lock();
			}
			else
			{
				if (pa.lock()->left->key == nd.lock()->key)
				{
					pa.lock()->left = next.lock();
					pa.lock()->left->parent = pa.lock();
				}
				else
				{
					pa.lock()->right = next.lock();
					pa.lock()->right->parent = pa.lock();
				}
			}
			next.lock()->left = left.lock();
		}
		else
		{
			if (next.lock()->right.use_count() == 0)
			{
				if (pa.lock()->left.use_count() == 0)
				{
					pa.lock()->right = next.lock();
					pa.lock()->right->parent = pa.lock();
				}
				else
				{
					if (pa.lock()->left->key == nd.lock()->key)
					{
						pa.lock()->left = next.lock();
						pa.lock()->left->parent = pa.lock();
					}
					else
					{
						pa.lock()->right = next.lock();
						pa.lock()->right->parent = pa.lock();
					}
				}
				next.lock()->left = left.lock();
				next.lock()->right = right.lock();
				next.lock()->parent->left.reset();
			}
			else
			{
				weak_ptr<Node>		nextParent = next.lock()->parent;
				weak_ptr<Node>		nextChildRight = next.lock()->right;

				if (pa.lock()->left.use_count() == 0)
				{
					pa.lock()->right = next.lock();
					pa.lock()->right->parent = pa.lock();
				}
				else
				{
					if (pa.lock()->left->key == nd.lock()->key)
					{
						pa.lock()->left = next.lock();
						pa.lock()->left->parent = pa.lock();
					}
					else
					{
						pa.lock()->right = next.lock();
						pa.lock()->right->parent = pa.lock();
					}
				}
				next.lock()->left = left.lock();
				nextParent.lock()->left = nextChildRight.lock();
				next.lock()->right = right.lock();
			}
		}
	}

}


int main()
{
#ifdef		FILE_INPUT_DEFINE
	// input from txt
	std::ifstream in(R"(C:\Temp\input.txt)");
	std::cin.rdbuf(in.rdbuf());
#endif		// FILE_INPUT_DEFINE
	string		str;
	int			n, key;

	cin >> n;
	cin.clear();
	cin.ignore();

	for (int i = 0; i < n; ++i)
	{
		cin >> str;
		if (str == "insert")
		{
			cin >> key;
			insert(key);
		}
		else if (str == "find")
		{
			cin >> key;
			if (find(key).first)
				cout << "yes\n";
			else
				cout << "no\n";
		}
		else if (str == "delete")
		{
			cin >> key;
			del(key);
		}
		else if (str == "print")
		{
			Order(root, Walk::eInOrder);
			cout << "\n";
			Order(root, Walk::ePreOrder);
			cout << "\n";
		}

		cin.clear();
		cin.ignore();
	}

	return 0;
}

Measured memory_bytes: 40564