#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int,int> P;

#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<(b);i++)
#define repr(i,b,a) for(int i=int(b);i>=(a);i--)
#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;

typedef struct node{
	int key=0;
	node *left=nullptr,*right=nullptr,*parent=nullptr;
	node(){};
	node(int s):key(s){};
}node;

class BinarySearchTree{
private:
	node *root = new node;
	vector<int> inorder,preorder;
	int num_of_nodes=0;

	void getInsertPosition(node *now,int value){
		if(now->key>value){
			if(now->left==nullptr){
				node *tmp = new node;
				tmp->key=value;
				now->left=tmp;
				tmp->parent=now;
			}else getInsertPosition(now->left, value);
		}else{
			if(now->right==nullptr){
				node *tmp = new node;
				tmp->key=value;
				now->right=tmp;
				tmp->parent=now;
			}else getInsertPosition(now->right, value);
		}
	}
	void dfs(node *now){
		preorder.push_back(now->key);
		if(now->left!=nullptr)dfs(now->left);
		inorder.push_back(now->key);
		if(now->right!=nullptr)dfs(now->right);
		return;
	}
	void printVector(vector<int> a){
		for(auto x:a)cout<<" "<<x;
		cout<<endl;
	}
	bool containValue(node *now, int target){
		if(now->key==target){
			// if(now->left!=nullptr&&now->right!=nullptr)cout<<"innner"<<endl;
			// else if(now->left==nullptr&&now->right!=nullptr)cout<<"left NULL"<<endl;
			// else if(now->left!=nullptr&&now->right==nullptr)cout<<"right NULL"<<endl;
			// else cout<<"leaf"<<endl;
			// cout<<now->right->key<<endl;
			return true;
		}

		// }return true;
		if(now->left!=nullptr&&target<now->key)return containValue(now->left, target);
		if(now->right!=nullptr&&target>now->key)return containValue(now->right, target);
		return false;
	}
	void deleteValue(node *now, int target){
		// cout<<now->key<<endl;
		if(now->key==target){
			if(now->left==nullptr&&now->right==nullptr){
				if(now->parent->left!=nullptr&&now->parent->left->key==target)now->parent->left=nullptr;
				else now->parent->right=nullptr;
				now->parent=nullptr;
				free(now);
			}else if(now->left!=nullptr&&now->right==nullptr){
				// cout<<now->key<<" !!!"<<endl;
				now->left->parent=now->parent;
				if(now->parent->left!=nullptr&&now->parent->left->key==target)now->parent->left=now->left;
				else now->parent->right=now->left;
				now->left=nullptr;
				now->parent=nullptr;
				free(now);
			}else if(now->left==nullptr&& now->right!=nullptr){
				// cout<<"!!!"<<endl;
				now->right->parent=now->parent;
				if(now->parent->left!=nullptr&&now->parent->left->key==target)now->parent->left=now->right;
				else now->parent->right=now->right;
				// cout<<now->right->key<<endl;
				now->right=nullptr;
				now->parent=nullptr;
				// cout<<"???"<<endl;
				free(now);
			}else{
				node *target = inorder_next(now->right);
				int tmp=target->key;
				deleteValue(now,tmp);
				now->key=tmp;
			}
		}else if(now->left!=nullptr&&target<now->key)deleteValue(now->left, target);
		else if(now->right!=nullptr&&target>now->key)deleteValue(now->right, target);

	}

	node *inorder_next(node *now){
		if(now->left!=nullptr) return inorder_next(now->left);
		return now;
	}
public:
	BinarySearchTree(){};
	void insert(int value){
		if(num_of_nodes==0){
			root->key=value;
			num_of_nodes++;
			return;
		}
		getInsertPosition(root, value);
	}
	
	void findValue(int value){
		cout<<(containValue(root,value)?"yes":"no")<<endl;
	}

	void deletevalue(int value){
		deleteValue(root,value);
	}

	void solve(){
		inorder.clear(),preorder.clear();
		dfs(root);
		printVector(inorder);
		printVector(preorder);
	}
};


int main()
{
	int n;
	cin>>n;
	BinarySearchTree BST;
	rep(i,n){
		string command;
		cin>>command;
		if(command=="insert"){
			int value;
			cin>>value;
			BST.insert(value);
		}else if(command=="find"){
			int value;
			cin>>value;
			BST.findValue(value);
		}else if(command=="delete"){
			int value;
			cin>>value;
			BST.deletevalue(value);
		}else{
			BST.solve();
		}
	}

	return 0;
}


Measured memory_bytes: 26548