#include<iostream>
#include <vector>
#include <string>
using namespace std;
const int NIL = -2;
class bintree{
public:
	int left;
	int right;
	int parent;
	int key;
	bintree():left(NIL),right(NIL),parent(NIL),key(0){
	}
	void clear(){
		left = NIL;
		right = NIL;
		parent = NIL;
		key = 0;
	}
};

void PrintID(int key) {
	cout << " " << key;
}
void TraversePrint_Preorder(vector<bintree> & T,int id) {
	bintree & t = T[id];
	PrintID(t.key);
	if (t.left != NIL) {
		TraversePrint_Preorder(T,t.left);
	}
	if (t.right != NIL) {
		TraversePrint_Preorder(T,t.right);
	}
}
void TraversePrint_Inorder(vector<bintree> & T,int id) {
	bintree & t = T[id];
	if (t.left != NIL) {
		TraversePrint_Inorder(T,t.left);
	}
	PrintID(t.key);
	if (t.right != NIL) {
		TraversePrint_Inorder(T,t.right);
	}
}
void TraversePrint_Postorder(vector<bintree> & T,int id) {
	bintree & t = T[id];
	if (t.left != NIL) {
		TraversePrint_Postorder(T,t.left);
	}
	if (t.right != NIL) {
		TraversePrint_Postorder(T,t.right);
	}
	PrintID(t.key);
}
int TraverseSearchId_Inorder(vector<bintree> & T,int id,int key) {
	bintree & t = T[id];
	if (t.key == key){
		return	id;
	} else if (key < t.key) {
		if (t.left != NIL) {
			return TraverseSearchId_Inorder(T,t.left,key);
		}
	} else {
		if (t.right != NIL) {
			return TraverseSearchId_Inorder(T,t.right,key);
		}
	}
	return NIL;
}
void TreeInsert(vector<bintree> & T,int &top,int z){
	int y = NIL;
	int x = top;
	while (x != NIL){
		y = x;
		if (T[z].key < T[x].key) {
			x = T[x].left;
		}
		else {
			x = T[x].right;
		}
	}
	T[z].parent = y;
	if (y == NIL) {
		top = z;
	} else if (T[z].key < T[y].key) {
		T[y].left = z;
	} else {
		T[y].right = z;
	}
}
//??????x????????即????????即?????即????????則????属??????????????????????????????????
int TreeMinimum(vector<bintree> & T,int id){
	while(T[id].left != NIL) {
		id = T[id].left;
	}
	return	id;
}
//????????蔵?卒蔵??即?????????x????測??????????????????卒?????????????測????????????????
//??????????????即????????則x????????則????????????????????卒??????NIL?????????
int TreeSuccessor(vector<bintree> & T,int id){
	if (T[id].right != NIL){
		return TreeMinimum(T,T[id].right);
	}
	int y = T[id].parent;
	while( y != NIL && T[y].right == id) {
		int x = y;
		y = T[y].parent;
	}
	return y;
}


void TreeDelete(vector<bintree> & T,int &top,int delid) {
	int y;
	//??????????????????y????賊??????????
	if (T[delid].left == NIL || T[delid].right == NIL) {
		/* z??????????????則??????????????卒????????\????????????z */
		y = delid;
	} else {
		y = TreeSuccessor(T,delid);	//z ?????????2???????????卒??????z????測???????
	}

	int x;
	//x???y???NIL??則?????????????????????y???????????????????????卒??????NIL????即????
	if (T[y].left != NIL) {
		x = T[y].left;
	} else {
		x = T[y].right;
	}

	//y????????即x???????????????????????卒???y???????????????
	//x???NIL?????????y???????????即???????蔵?????????其?????????
	if (x != NIL) {
		T[x].parent = T[y].parent;	//z???????????????????????卒???
	}
	if (T[y].parent == NIL) {
		top = x;
	} else if(y == T[T[y].parent].left) {
		T[T[y].parent].left = x;
	} else {
		T[T[y].parent].right = x;
	}

	/* z????測???????????????????????????即??????y????????????z????則??????????*/
	if (y != delid) {
		T[delid].key = T[y].key;
	}
	T[y].clear();
}




int main(){
	vector<bintree> T;
	int n;
	cin >> n;
	T.resize(n);
	int top = NIL;
	for(int z=0;z<n;++z) {
		string op;
		cin >> op;
		if (op == "insert") {
			cin >> T[z].key;
			TreeInsert(T,top,z);
		} else if (op == "find") {
			int k;
			cin >> k;
			int find = TraverseSearchId_Inorder(T,top,k);
			const char *findmsg[] = {"no","yes"};
			cout << findmsg[(find==NIL) ? 0 : 1] << std::endl;
		} else if (op == "delete") {
			int k;
			cin >> k;
			int find = TraverseSearchId_Inorder(T,top,k);
			if (find != NIL) {
				TreeDelete(T,top,find);
			}
		} else if (op == "print") {
			TraversePrint_Inorder(T,top);
			cout << std::endl;
			TraversePrint_Preorder(T,top);
			cout << std::endl;
		}
	}
}

Measured memory_bytes: 8984