#include <iostream>
#include <string>
#include <functional>
#include <cassert>
#include <sstream>
#include <iomanip>
using namespace std;

template <class T, class C = std::less<T> >
class binary_tree {
public:
	class node {
		node *parent, *children[2];

		void unlink(){
			for(int k = 0; k < 2; ++k){
				if(parent && parent->children[k] == this){
					parent->children[k] = nullptr;
				}
				if(children[k]){
					children[k]->parent = nullptr;
					children[k] = nullptr;
				}
			}
			parent = nullptr;
		}
		
		void link(int k, node *child){
			if(!child){ return; }
			assert(!children[k]);
			assert(!child->parent);
			children[k] = child;
			child->parent = this;
		}

		void splice_out(){
			assert(!children[0] || !children[1]);
			if(!children[0] && !children[1]){
				unlink();
			}
			else{
				node *p = parent;
				node *q = children[children[0] ? 0 : 1];
				int k = p->children[0] == this ? 0 : 1;
				unlink();
				p->link(k, q);
			}
		}
		
		node *successor(){
			node *current = children[1];
			assert(current);
			while(current->children[0]){
				current = current->children[0];
			}
			return current;
		}
	protected:
		node() {
			parent = nullptr;
			children[0] = children[1] = nullptr;
		}
	public:
		virtual const T &get(){
			throw std::runtime_error("unsupported");
		}
		virtual ~node(){
			delete children[0];
			delete children[1];
		}

		friend class binary_tree;
	};
private:
	class value_node : public node {
		const T value;
		value_node(const T &val) : value(val) {}
	public:
		const T& get() override { return value; }

		friend class binary_tree;
	};

	binary_tree(const binary_tree &) = delete;
	binary_tree(binary_tree &&) = delete;
	binary_tree &operator= (const binary_tree &) = delete;
	binary_tree &operator= (binary_tree &&) = delete;

	node * const dummy;
	C comp;

	void inorder(stringstream &ss, node *current){
		if(!current){ return; }
		inorder(ss, current->children[0]);
		ss << ' ' << current->get();
		inorder(ss, current->children[1]);
	}

	void preorder(stringstream &ss, node *current){
		if(!current){ return; }
		ss << ' ' << current->get();
		preorder(ss, current->children[0]);
		preorder(ss, current->children[1]);
	}

public:
	explicit binary_tree(C comp_ = C()) : dummy(new node()), comp(comp_) {
	}

	void insert(const T &value){
		node *current = dummy;
		while(true){
			int k = current == dummy || comp(value, current->get()) ? 0 : 1;
			if(!current->children[k]){
				current->link(k, new value_node(value));
				return;
			}
			current = current->children[k];
		}
	}

	bool erase(const T &value){
		node *current = dummy->children[0];
		while(current){
			if(comp(value, current->get())){
				current = current->children[0];
			}
			else if(comp(current->get(), value)){
				current = current->children[1];
			}
			else{
				if(current->children[0] && current->children[1]){
					node *s = current->successor();
					s->splice_out();
					node *p = current->parent;
					int k = p->children[0] == current ? 0 : 1;
					node *cs[] = {current->children[0], current->children[1]};
					current->unlink();
					p->link(k, s);
					for(int i = 0; i < 2; ++i){
						s->link(i, cs[i]);
					}
				}
				else{
					current->splice_out();
				}
				delete current;
				return true;
			}
		}
		return false;
	}
	
	bool find(const T &value){
		node *current = dummy->children[0];
		while(current){
			if(comp(value, current->get())){
				current = current->children[0];
			}
			else if(comp(current->get(), value)){
				current = current->children[1];
			}
			else{
				return true;
			}
		}
		return false;
	}
	
	void print(){
		stringstream in, pre;
		inorder(in, dummy->children[0]);
		preorder(pre, dummy->children[0]);

		cout << in.str() << '\n' << pre.str() << '\n';
	}
	
	~binary_tree(){
		delete dummy;
	}
};

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	binary_tree<int> tr;
	int n;
	cin >> n;
	string cmd;
	for(int i = 0; i < n && cin >> cmd; ++i){
		if(cmd == "print"){
			tr.print();
		}
		else{
			int arg;
			cin >> arg;
			
			if(cmd == "insert"){
				tr.insert(arg);
			}
			else if(cmd == "find"){
				bool res = tr.find(arg);
				cout << (res ? "yes\n" : "no\n");
			}
			else{
				tr.erase(arg);
			}
		}
	}
}



Measured memory_bytes: 34908