#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <deque>
#include <algorithm>
#include <numeric>
#include <utility>
#include <complex>
#include <functional>

using namespace std;

struct Tree
{
	int key;
	Tree *left;
	Tree *right;
	Tree *parent;
};

// Tree::Tree(int k, Tree *l, Tree *r): key(k), left(l), right(r){}

void printTreeMid(Tree *tree)
{
	// cout << "mid" << endl;
	if (tree == nullptr)
		return;
	printTreeMid(tree->left);
	printf(" %d", tree->key);
	printTreeMid(tree->right);
}

void printTreePro(Tree *tree)
{
	if (tree == nullptr)
		return;
	printf(" %d", tree->key);
	printTreePro(tree->left);
	printTreePro(tree->right);
}

void insert(Tree *tree, int key)
{
	Tree *y = nullptr;
	Tree *x = tree->right;
	while (x != nullptr)
	{
		// cout << "in while" << endl;
		y = x;
		if (x->key < key)
		{
			x = x->right;
		}
		else
		{
			x = x->left;
		}
	}
	Tree *z = (Tree *)calloc(1, sizeof(Tree));
	z->parent = y;
	z->key = key;
	z->left = nullptr;
	z->right = nullptr;
	if (z->parent == nullptr)
	{
		tree->right = z;
		return;
	}
	if (y->key < key)
	{
		y->right = z;
	}
	else
	{
		y->left = z;
	}
}

// int findNode(Tree *tree, int k) {
// 	if(tree == nullptr) {
// 		cout << "no" << endl;
// 		return 0;
// 	}
// 	if(tree->key == k) {
// 		cout << "yes" << endl;
// 		return 1;
// 	} else if(tree->key > k) {
// 		return findNode(tree->left, k);
// 	} else {
// 		return findNode(tree->right, k);
// 	}
// }

Tree* findNode(Tree *tree, int k)
{
	while (tree != nullptr)
	{
		if (tree->key == k)
		{
			return tree;
		} else if(tree->key > k) {
			tree = tree->left;
		} else {
			tree = tree->right;
		}
	}
	return nullptr;
}

Tree* getNext(Tree *node) {
	if(node->right != nullptr) {
		Tree *t = node->right;
		while(t->left != nullptr) {
			t = t->left;
		}
		return t;
	}
	return nullptr;
	// else {

	// }
}

void deleteNode(Tree *root, int k) {
	Tree *z = findNode(root, k);
	Tree *delNode, *x;
	if(z->left == nullptr || z->right == nullptr) {
		delNode = z; //not null
	} else {

		delNode = getNext(z); //not null
	}

	// 子供がないときnull, 子供がいる時それ
	if(delNode->left != nullptr) {
		x = delNode->left; //x != null
	} else {

		x = delNode->right;
	}

	if(x != nullptr) {
		x->parent = delNode->parent;
	}

	if(delNode->parent == nullptr) {
		root->right = x;
	} else if(delNode == delNode->parent->left) {
		delNode->parent->left = x;
	} else {
		delNode->parent->right = x;
	}

	if(delNode != z) {
		z->key = delNode->key;
	}
}

//8:00
int main()
{
	int n;
	cin >> n;
	Tree *NIL = new Tree();
	NIL->right = nullptr;
	for (size_t i = 0; i < n; i++)
	{
		string s;
		cin >> s;
		if (s == "insert")
		{
			int k;
			scanf("%d", &k);
			// if(tree != nullptr) {
			// 	cout << "root: " << tree->key;
			// }
			insert(NIL, k);
		}
		else if (s == "print")
		{
			printTreeMid(NIL->right);
			cout << endl;
			printTreePro(NIL->right);
			cout << endl;
		}
		else if (s == "find")
		{
			int k;
			scanf("%d", &k);
			if(findNode(NIL, k) == nullptr) {
				cout << "no" << endl;
			} else {
				cout << "yes" << endl;
			}
		} else {
			int k;
			scanf("%d", &k);
			deleteNode(NIL, k);
		}
	}
}



Measured memory_bytes: 22008