#include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

struct OwnNode
{
	int key;
	OwnNode* parent;
	OwnNode* left;
	OwnNode* right;

	OwnNode(int key):key(key),parent(nullptr),left(nullptr),right(nullptr){}
};

void Insert(OwnNode* &top, OwnNode* &z)
{
	OwnNode* y = nullptr;
	OwnNode* x = top;

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

	z->parent = y;

	if (y == nullptr)
	{
		top = z;
	}
	else if (z->key < y->key)
	{
		y->left = z;
	}
	else
	{
		y->right = z;
	}
}

OwnNode* Find(OwnNode* &top, int key)
{
	if (top == nullptr)
	{
		return nullptr;
	}
	if (top->key == key)
	{
		return top;
	}
	else
	{
		if (top->key < key)
		{
			if (top->right == nullptr)
			{
				return nullptr;
			}
			else
			{
				return Find(top->right, key);
			}
		}
		else
		{
			if (top->left == nullptr)
			{
				return nullptr;
			}
			else
			{
				return Find(top->left, key);
			}
		}
	}
}

void Delete(OwnNode* &top, int key)
{
	OwnNode* z = Find(top, key);

	if (z == nullptr)
	{
		//cout << "return" << endl;
		return;
	}
	
	if (z->left == nullptr&&z->right == nullptr)
	{
		if (z->parent == nullptr)
		{
			top = nullptr;
		}
		else
		{
			if (z->parent->key < key)
			{
				z->parent->right = nullptr;
			}
			else
			{
				z->parent->left = nullptr;
			}
		}
		delete z;
	}
	else if (z->left != nullptr&&z->right != nullptr)
	{
		OwnNode* temp = z->right;
		while (temp->left != nullptr)
		{
			temp = temp->left;
		}
		z->key = temp->key;

		if (temp->right == nullptr)
		{
			if (temp->parent->key ==temp->key)
			{
				temp->parent->right = nullptr;
			}
			else
			{
				temp->parent->left = nullptr;
			}
		}
		else
		{
			if (temp->parent->key == temp->key)
			{
				temp->parent->right = temp->right;
				temp->right->parent = temp->parent;
			}
			else
			{
				temp->parent->left = temp->right;
				temp->right->parent = temp->parent;
			}
		}
		delete temp;
	}
	else
	{
		if (z->parent == nullptr)
		{
			if (z->right != nullptr)
			{
				top = z->right;
			}
			else
			{
				top = z->left;
			}
		}
		else
		{
			if (z->parent->key < key)
			{
				if (z->left != nullptr)
				{
					z->parent->right = z->left;
					z->left->parent = z->parent;
				}
				else
				{
					z->parent->right = z->right;
					z->right->parent = z->parent;
				}
			}
			else
			{
				if (z->left != nullptr)
				{
					z->parent->left = z->left;
					z->left->parent = z->parent;
				}
				else
				{
					z->parent->left = z->right;
					z->right->parent = z->parent;
				}
			}
		}
		delete z;
	}
}

void PrintPre(OwnNode* top)
{
	printf(" %d", top->key);
	if (top->left != nullptr)
	{
		PrintPre(top->left);
	}
	if (top->right != nullptr)
	{
		PrintPre(top->right);
	}
}

void PrintIn(OwnNode* top)
{
	if (top->left != nullptr)
	{
		PrintIn(top->left);
	}
	printf(" %d", top->key);
	if (top->right != nullptr)
	{
		PrintIn(top->right);
	}
}

void Print(OwnNode* top)
{
	if (top == nullptr)
	{
		return;
	}
	PrintIn(top);
	printf("\n");
	PrintPre(top);
	printf("\n");
}

int main()
{
	OwnNode* T;
	OwnNode* z;
	T = nullptr;

	int m;
	cin >> m;

	for (int i = 0; i < m; i++)
	{
		char command[10];
		//int key;

		scanf("%s", command);

		//string command;

		//cin >> command;

		if (command[0] == 'i')
		{
			int key;
			scanf("%d", &key);
			z = new OwnNode(key);
			Insert(T, z);
		}
		else if (command[0] == 'f')
		{
			int key;
			cin >> key;
			printf(Find(T, key) != nullptr ? "yes\n" : "no\n");
		}
		else if (command[0] == 'd')
		{
			int key;
			cin >> key;
			Delete(T, key);
		}
		else
		{
			Print(T);
		}
	}

	return 0;
}


Measured memory_bytes: 21948