#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>

using namespace std;

constexpr int INFI = 2000000001;

class node{
public:
	int id;
	node* left, * right;
	node*  parent;
	int sibling;
	int depth;
	int height;
};

bool operator<=(node a, node b) {
	return a.id <= b.id;
}
bool operator<(node a, node b) {
	return a.id < b.id;
}

bool operator==(node a, node b) {
	return a.id == b.id;
}

bool operator!=(node a, node b) {
	return a.id != b.id;
}

void add(node* root,int a) {
	node *newnode = new node;
	node* now = root->left;
	newnode->id = a;
	newnode->left = NULL;
	newnode->right = NULL;
	if (now == NULL) {
		newnode->parent = root;
		root->left = newnode;
		return;
	}
	while (1) {
		if (now->id > a) {
			if (now->left == NULL) {
				now->left = newnode;
				newnode->parent = now;
				return;
			}
			else {
				now = now->left;
			}
		}
		else {
			if (now->right == NULL) {
				now->right = newnode;
				newnode->parent = now;
				return;
			}
			else {
				now = now->right;
			}
		}
	}
}


string postorder(node* nod) {
	string msg="";
	if (nod->left->id != -1) {
		msg += postorder(nod->left);	
	}
	if (nod->right->id != -1) {
		msg += postorder(nod->right);	
	}
	msg += to_string(nod->id) + " ";
	return msg;
}

void preorder(node* root) {
	if (root == NULL)return;
	cout << " "<<root->id;
	preorder(root->left);
	preorder(root->right);
}

void inorder(node* root) {
	if (root == NULL)return;
	inorder(root->left);
	cout <<" "<< root->id;
	inorder(root->right);
}

bool find(node* root,int a) {
	if (root == NULL)return false;
	if (root->id == a)return true;
	if (root->id > a)return find(root->left, a);
	if (root->id < a)return find(root->right, a);
}

void del(node* root, int a) {
	root = root->left;
	while (1) {
		if (root->id == a) {
			break;
		}
		else if (root->id > a) {
			root = root->left;
		}
		else {
			root = root->right;
		}
		if (root == NULL)return;
	}
	if (root->id > root->parent->id) {
		if (root->left == NULL && root->right == NULL) {
			root->parent->right = NULL;
			free(root);
		}
		else if (root->left != NULL && root->right != NULL) {
			node* nod = root->right;
			while (nod->left != NULL) {
				nod = nod->left;
			}
			root->id = nod->id;
			if (nod->parent == root) {
				root->right = NULL;
			}
			else {
				nod->parent->left = NULL;
			}
			free(nod);
		}
		else if (root->left != NULL) {
			root->parent->right = root->left;
			root->left->parent = root->parent;
			free(root);
		}
		else {
			root->parent->right = root->right;
			root->right->parent = root->parent;
			free(root);
		}
	}
	else {
		if (root->left == NULL && root->right == NULL) {
			root->parent->left = NULL;
			free(root);
		}
		else if (root->left != NULL && root->right != NULL) {
			node* nod = root->right;
			while (nod->left != NULL) {
				nod = nod->left;
			}
			root->id = nod->id;
			if (nod->parent == root) {
				root->right = NULL;
			}
			else {
				nod->parent->left = NULL;
			}
			free(nod);
		}
		else if (root->left != NULL) {
			root->parent->left = root->left;
			root->left->parent = root->parent;
			free(root);
		}
		else {
			root->parent->left = root->right;
			root->right->parent = root->parent;
			free(root);
		}
	}
}



int main() {
	int n,tmp;
	string msg;
	cin >> n;
	node* root = new node;
	root->left = root->right = NULL;
	root->id = INFI;
	for (int i = 0;i < n;i++) {
		cin >> msg;
		if (msg == "insert") {
			cin >> tmp;
			add(root, tmp);
		}
		else if (msg == "print") {
			inorder(root->left);
			cout << endl;
			preorder(root->left);
			cout << endl;
		}
		else if(msg == "find"){
			cin >> tmp;
			if (find(root->left, tmp))cout << "yes\n";
			else cout << "no\n";
		}
		else {
			cin >> tmp;
			del(root, tmp);
		}
	}
}






Measured memory_bytes: 28112