#include<iostream>
using namespace std;

struct Node{
	Node *left;
	Node *right;
	int data;
	Node(){left=right=0;}
	Node(int tmp,Node*l=0,Node*r=0){
		left=l;
		right=r;
		data=tmp;
	}
};

class tree{
	private:
		Node *first;
	public:
		tree():first(0){
		}
		void insert(int tmp){
			if(!first){
				first=new Node(tmp);
				return;
			}
			Node *link = first;
			while(true){
				if(link->data>tmp){
					if(link->left)link=link->left;
					else{
					link->left=new Node(tmp); 
					return;
					} 
				}
				else if(link->data<tmp){
					if(link->right)link=link->right;
					else{
					link->right=new Node(tmp); 
					return;
					} 
				}
				else return;
			}}
		void find(int tmp){
			if(!first){
				cout<<"no"<<endl;
				return;
			}
			Node *link = first;
			while(true){
				if(link->data>tmp){
					if(link->left)link=link->left;
					else{
					cout<<"no"<<endl;
					return;
					} 
				}
				else if(link->data<tmp){
					if(link->right)link=link->right;
					else{
					cout<<"no"<<endl;
					return;
					} 
				}
				
				else {
				cout<<"yes"<<endl;
				return;	
				}}
		}
		void pre(Node *tmp){
			if(!tmp)return;
			cout<<' '<<tmp->data;
			pre(tmp->left);
			pre(tmp->right);
		}
		void in(Node *tmp){
			if(!tmp)return;
			in(tmp->left);
			cout<<' '<<tmp->data;
			in(tmp->right);	
		}
		void print(){
			in(first);
			cout<<endl;
			pre(first);
			cout<<endl;
		} 
		void del(int tmp){
			if(!first)return;
			Node *link = first;
			Node *parent=NULL;
			while(true){
			if(link->data>tmp){
			parent=link;
			if(link->left)link=link->left;
			else return;
			}
			else if(link->data<tmp){
			parent=link;
			if(link->right)link=link->right;
			else return;
			}
			else break;
			}
			if(link->left&&link->right){
			
			Node *bedel = link->right;
			parent = NULL; 
			while(bedel->left){
				parent=bedel; 
				bedel=bedel->left;
			}
			
			if(bedel->right){
				if(parent){
					link->data=bedel->data;
					parent->left=bedel->right;
					delete bedel;
					return;
				}else{
					link->data=bedel->data;
					link->right=bedel->right;
					delete bedel;
					return;
				}
			}
			else{
				if(parent){
					link->data=bedel->data;
					parent->left=NULL;
					delete bedel;
					return; 
				}
				else{
					link->data=bedel->data;
					link->right=NULL;
					delete bedel;
					return;
				}
			}
			
			}
			else if(link->left){
			if(parent){
				if(parent->left==link){
					parent->left=link->left;
					delete link;
				}else{
					parent->right=link->left;
					delete link;
					return;
				}
			}else{
				first=first->left;
				delete link;
				return;
			}
			}
			else{
				if(parent){
				if(parent->left==link){
					parent->left=link->right;
					delete link;
					return;
				}else{
					parent->right=link->right;
					delete link;
					return;
				}
			}else{
				first=first->right;
				delete link;
				return;
			}
			}
		}
};
int main(){
	char s[100];
	int n,tmp;
	tree a;
	cin>>n;
	while(n--){
		cin>>s;
		switch(s[0]){
			case 'i':cin>>tmp;a.insert(tmp);break;
			case 'p':a.print();break;
			case 'f':cin>>tmp;a.find(tmp);break;
			case 'd':cin>>tmp;a.del(tmp);break;
			default:break;
		}
	}
	return 0;
}


Measured memory_bytes: 15604