#include <memory>
#include <iostream>
#include <algorithm>
#include <string>

class Node{
public:
	Node(int v = 0):value(v), parent(nullptr), left(nullptr), right(nullptr){};
	void print(){std::cout << " ";inorder_express();std::cout<<"\n ";preorder_express();std::cout<<'\n';};
	std::shared_ptr<Node> get_p(){
		return parent;
	}
	int to_i(){return value;}
	Node &upper(){
		return (*parent);
	}
	Node &search(const int &v){
		if(value > v and left){
			return left->search(v);
		}else if(value < v and right){
			return right->search(v);
		}else{
			return *this;
		}
	}
	void insert(const int &key, const std::shared_ptr<Node> &par = nullptr){
		if (value > key){
			if (left){
				left->insert(key, left);
			}else{
				left = std::make_shared<Node>(key);
				left->parent = par;
			}
		}else{
			if(right){
				right->insert(key, right);
			}else{
				right = std::make_shared<Node>(key);
				right->parent = par;
			}
		}
	}
	bool find(const int &v){
		if (value != v){
			return (search(v).value  == v);
		}else{
			return true;
		}
	}
	void delete_node(const int &key){
		search(key).del_node();
	}
private:
	int value;
	std::shared_ptr<Node> parent, left, right;
	void inorder_express(){
		if (left){left->inorder_express();std::cout << " ";};
		std::cout << value;
		if (right){std::cout << " ";right->inorder_express();};
	}
	void preorder_express(){
		std::cout << value;
		if (left){std::cout << " ";left->preorder_express();};
		if (right){std::cout << " ";right->preorder_express();};
	}
	void ref_clear(){
		parent = nullptr;
		left = nullptr;
		right = nullptr;
	}
	void del_node(){
		if (left and right){
			Node* target = right->leften();
			value = target->value;
			target->del_node();
			//std::cout << value << " del3" << std::endl;
		}else if (left or right){
			std::pair<std::pair<std::shared_ptr<Node>, std::shared_ptr<Node>>, int> pair;
			if (left){
				pair = left->del2();
				left->ref_clear();
			}else{
				pair = right->del2();
				right->ref_clear();
			}
			left = pair.first.first;
			right = pair.first.second;
			value = pair.second;
			//std::cout << value << " del2" << std::endl;
		}else{
			if (parent) parent->del(value);
			//std::cout << value << " del1" << std::endl;
		}
	}
	Node *leften(){
		if (left){
			return left->leften();
		}else{
			return this;
		}
	}
	std::pair<std::pair<std::shared_ptr<Node>, std::shared_ptr<Node>>, int> del2(){
		parent = nullptr;
		return make_pair(make_pair(left, right), value);
	}
	int &del3(){
		if (right){
			if ((parent->value) > (right->value)){
				parent->left = right;
			}else{
				parent->right = right;
			}
		}
		ref_clear();
		return value;
	}
	void del(const int key){
		if (value > key){
			left->ref_clear();
			left = nullptr;
		}else{
			right->ref_clear();
			right = nullptr;
		}
	}
};
int main(){
	int m;
	std::cin >> m;
	int num{0};
	Node root;
	for (auto i = 0; i < m; ++i){
		std::string order;
		int key;
		std::cin >> order;
		if (order == "insert"){
			std::cin >> key;
			if (num == 0){
				root = Node(key);
			}else{
				root.insert(key);
			}
			++num;
		}else if (order == "find"){
			std::cin >> key;
			if (num == 0){
			std::cout << "no\n";
			}else if (root.find(key)){
				std::cout << "yes\n";
			}else{
				std::cout<< "no\n";
			}
		}else if (order == "print"){
			if (num != 0){
				root.print();
			}
		}else{
			std::cin >> key;
			root.delete_node(key);
			--num;
		}
	}
}

Measured memory_bytes: 38740