#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
using namespace std;

struct tree{
	int value;
	tree *Left;
	tree *Right;
};

tree *root;

class binary_tree{
	public:
		binary_tree();
		int insert1(int);
		int insert11(tree *, int);
		tree *insert2(tree *, tree *);
		tree *find(int);
		tree *find2(int);
		tree *mini(tree *);
		int delete2(int);
		int pretraverse(tree *);
		int intraverse(tree *);
		int posttraverse(tree *);
};

binary_tree::binary_tree(){
	root = NULL;
}

tree * binary_tree::insert2(tree *node, tree *newnode){
	if (node == NULL)
		node = newnode;
	else
		if ( newnode->value > node->value)
			if (node->Right == NULL)
				node->Right = newnode;
			else
				insert2(node->Right, newnode);
		else
			if (node->Left == NULL)
				node->Left =newnode;
			else
				insert2(node->Left, newnode);
	return node;
}

int binary_tree::insert1(int n){
	tree *p = root, *newnode;

	newnode = new tree;
	newnode->value = n;
	newnode->Left = NULL;
	newnode->Right = NULL;
	root = insert2(p, newnode);
	return 0;
}

tree *binary_tree::find(int n){
	tree *p=root;
	int tmp;
	while (p != NULL){
		tmp = p->value;
		if (tmp == n) 
			return p;
		else if (tmp < n)
			p = p->Right; 
		else if (tmp > n)
			p = p->Left;
	}
	return NULL;
}
int binary_tree::pretraverse(tree *p = root){
	if (root==NULL){}
	else if (p != NULL){
		cout << " " << p->value;
		pretraverse(p->Left);
		pretraverse(p->Right);
	}
	return 0;
}

int binary_tree::intraverse(tree *p = root){
	if (root == NULL){}
	else if (p != NULL){
		intraverse(p->Left);
		cout << " " << p->value;
		intraverse(p->Right);
	}
	return 0;
}

int binary_tree::posttraverse(tree *p = root){
	if (root == NULL){}
	else if (p != NULL){
		posttraverse(p->Left);
		posttraverse(p->Right);
		cout << " " << p->value;
	}
	return 0;
}


tree *binary_tree::mini(tree *marker){
	tree *temp, *p1, *p2, *p3;

	if (marker->Right == NULL){
		return NULL;
	}
	else{
		p1 = NULL;
		p2 = marker;
		p3 = marker->Right;

		while (p3 != NULL){
			p1 = p2;
			p2 = p3;
			p3 = p3->Left;
		}

		temp = new tree;
		temp->value = NULL;
		temp->Left = p1;
		temp->Right = p2;
	}
	return temp;
}
tree *binary_tree::find2(int n){
	tree *temp, *p1, *p2, *p3;
	int flg;

	p1 = root;
	p2 = root;
	while (p2 != NULL){
		if (p2->value == n){
			temp = new tree;
			temp->value=flg;
			temp->Left = p1;
			temp->Right = p2;
			return temp;
		}
		else if (p2->value < n){
			p1 = p2;
			p2 = p2->Right;
			flg = 0;
		}
		else{
			p1 = p2;
			p2 = p2->Left;
			flg = 1;
		}
	}
	return NULL;
}


int binary_tree::delete2(int key){

	tree *parent, *marker, *temp;

	temp = find2(key);
	if (temp == NULL){
		return NULL;
	}
	else{
		parent = temp->Left;
		marker = temp->Right;
	}

	if (marker->Right == NULL && marker->Left == NULL){
		if (parent == root)
			parent = NULL;
		else if (parent->Left == marker)
			parent->Left = NULL;
		else if (parent->Right == marker)
			parent->Right = NULL;
		delete marker;
	}
	else if (marker->Right == NULL){
		if (parent->Left == marker)
			parent->Left = marker->Left;
		else if(parent->Right == marker)
			parent->Right = marker->Left;
		delete marker;
	}
	else if (marker->Left == NULL){
		if (parent->Left == marker)
			parent->Left = marker->Right;
		else if(parent->Right == marker)
			parent->Right = marker->Right;
		delete marker;
	}
	else{
		tree *right_min, *right_min_parent;

		temp = mini(marker);
		right_min_parent = temp->Left;
		right_min = temp->Right;

		if (marker == right_min_parent){
			marker->Right = right_min->Right;
		}
		else{
			right_min_parent->Left = right_min->Right;
		}
		marker->value = right_min->value;
		delete right_min;
	}

	return 0;
}

int str2int(string text){
	stringstream buf;
	int n;

	buf<<text;
	buf>>n;
	return n;
}
string int2str(int n){
	stringstream buf;
	string text;

	buf<<n;
	buf>>text;
	return text;
}

int main(void){
	int n;
	string text,text1;
	binary_tree btree;
	tree *p = root;

	cin >> n;
	getline(cin, text);
	for (int i=0; i<n; i++){
		getline(cin, text);
		int temp=0;
		switch (text[0]){
			case 'i':
				temp = str2int(text.substr(7));
				btree.insert1(temp);
				break;
			case 'd':
				temp = str2int(text.substr(7));
				btree.delete2(temp);
				break;
			case 'f':
				temp = str2int(text.substr(5));
				if (btree.find(temp)==NULL)
					cout<<"no"<<endl;
				else
					cout<<"yes"<<endl;
				break;
			case 'p':
				btree.intraverse();
				cout << endl;
				btree.pretraverse();
				cout << endl;
				break;
		}
	}
	return 0;
} 

Measured memory_bytes: 13736