#include<iostream>
using namespace std;

struct Node {
	int key;
	Node *parent, *left, *right;
};

Node *NIL, *root;

void printPreOrder(Node *u);
void printInOrder(Node *u);
void insertTree(int k);
void isFound(int k);
void deleteNode(int k);
Node* getSuccessor(Node *u);
Node* getMinimum(Node *u);

int main() {
	int i, j, n, k;
	char com[10];
	cin >> n;
	
	for (i = 0; i < n; i++) {
		cin >> com;
		if (com[0] == 'i') {
			cin >> k;
			insertTree(k);
		} else if (com[0] == 'f') {
			cin >> k;
			isFound(k);
		} else if(com[0] == 'd'){
			cin >> k;
			deleteNode(k);
		} else {
			printInOrder(root);
			cout << endl;
			printPreOrder(root);
			cout << endl;
		}
	}
	return 0;
}

void printPreOrder(Node *u) {
	if (u == NIL)return;
	cout << " " << u->key;
	printPreOrder(u->left);
	printPreOrder(u->right);
}

void printInOrder(Node *u) {
	if (u == NIL)return;
	printInOrder(u->left);
	cout << " " << u->key;
	printInOrder(u->right);
}

void insertTree(int k) {
	Node *y = NIL;
	Node *x = root;
	Node *z;
	z = new Node;
	z->key = k;
	z->left = NIL;
	z->right = NIL;
	while (x != NIL) {
		y = x;
		if (z->key < x->key)x = x->left;
		else x = x->right;
	}
	z->parent = y;
	if (y == NIL)root = z;
	else if (z->key < y->key)y->left = z;
	else y->right = z;
}

void isFound(int k) {
	Node *x = root;
	while (x != NIL) {
		if (x->key == k) {
			cout << "yes\n";
			return;
		}
		else if (x->key < k)x = x->right;
		else x = x->left;
	}
	cout << "no\n";
	return;
	return;
}

void deleteNode(int k) {
	Node *x, *y, *z = root;
	while (z != NIL && z->key != k) {
		if (k < z->key)z = z->left;
		else z = z->right;
	}
	if (z == NIL)return;

	if (z->left == NIL || z->right == NIL)y = z;
	else y = getSuccessor(z);

	if (y->left != NIL)x = y->left;
	else x = y->right;

	if (x != NIL)x->parent = y->parent;

	if (y->parent == NIL)root = x;
	else if (y == y->parent->left)y->parent->left = x;
	else y->parent->right = x;

	if (y != z)z->key = y->key;

	delete y;

}

Node* getSuccessor(Node *u) {
	if (u->right != NIL)return getMinimum(u->right);
	Node *y = u->parent;
	while (y != NIL && u == y->right) {
		u = y;
		y = y->parent;
	}
	return y;
}

Node* getMinimum(Node *u) {
	while (u->left != NIL) {
		u = u->left;
	}
	return u;
}