#include <iostream>
#include <string>
#include <cassert>
using namespace std;

struct TTree
{
private:
	struct TNode
	{
	public:
		int Id;
		TNode* Parent;
		TNode* LChild;
		TNode* RChild;
		TNode(int Value)
		{
			Id = Value;
			Parent = NULL;
			LChild = NULL;
			RChild = NULL;
		}
	};
	TNode* Root;
	void Delete(const TNode* Node)
	{
		if(Node == NULL) return;
		Delete(Node->LChild);
		Delete(Node->RChild);
		delete Node;
	}
	void InorderOutput(const TNode* Node)const
	{
		if(Node == NULL) return;
		InorderOutput(Node->LChild);
		cout << ' ' << Node->Id;
		InorderOutput(Node->RChild);
	}
	void PreorderOutput(const TNode* Node)const
	{
		if(Node == NULL) return;
		cout << ' ' << Node->Id;
		PreorderOutput(Node->LChild);
		PreorderOutput(Node->RChild);
	}
	void Insert(TNode* z)
	{
		assert(z != NULL);
		TNode* y = NULL;
		TNode* x = Root;
		while(x != NULL){
			y = x;
			if(z->Id < x->Id){
				x = x->LChild;
			}else{
				x = x->RChild;
			}
		}
		z->Parent = y;
		if(y == NULL){
			Root = z;
		}else if(z->Id < y->Id){
			y->LChild = z;
		}else{
			y->RChild = z;
		}
	}
	bool Find(TNode* Node, int Value)const
	{
		if(Node == NULL) return false;
		if(Node->Id == Value){
			return true;
		}else if(Node->Id > Value){
			return Find(Node->LChild, Value);
		}else{
			return Find(Node->RChild, Value);
		}
	}
	const TNode* GetNode(const TNode* Node, int Value)const
	{
		if(Node == NULL) return NULL;
		if(Node->Id == Value){
			return Node;
		}else if(Node->Id > Value){
			return GetNode(Node->LChild, Value);
		}else{
			return GetNode(Node->RChild, Value);
		}
	}
	TNode* GetNode(TNode* Node, int Value)
	{
		if(Node == NULL) return NULL;
		if(Node->Id == Value){
			return Node;
		}else if(Node->Id > Value){
			return GetNode(Node->LChild, Value);
		}else{
			return GetNode(Node->RChild, Value);
		}
	}
	TNode* GetInorderNextNode(TNode* Node)
	{
		if(Node->LChild == NULL && Node->RChild == NULL) return Node;
		if(Node->LChild == NULL) return GetInorderNextNode(Node->RChild);
		return GetInorderNextNode(Node->LChild);
	}
	void Erase(TNode* Node)
	{
		if(Node->LChild == NULL && Node->RChild == NULL){
			if(Node->Parent->LChild == Node){
				Node->Parent->LChild = NULL;
			}else{
				Node->Parent->RChild = NULL;
			}
			delete Node;
		}else if(Node->LChild != NULL && Node->RChild != NULL){
			assert(Node->RChild != NULL);
			TNode* InorderNextNode = GetInorderNextNode(Node->RChild);
			Node->Id = InorderNextNode->Id;
			Erase(InorderNextNode);
		}else{
			TNode* Child = NULL;
			if(Node->LChild){
				Child = Node->LChild;
			}else{
				Child = Node->RChild;
			}
			if(Node->Parent->LChild == Node){
				Node->Parent->LChild = Child;
			}else{
				Node->Parent->RChild = Child;
			}
			Child->Parent = Node->Parent;
			delete Node;
		}
	}
public:
	TTree()
	{
		Root = NULL;
	}
	~TTree()
	{
		Delete(Root);
	}
	void Insert(int Id)
	{
		Insert(new TNode(Id));
	}
	void Find(int Id)const
	{
	    if(GetNode(Root, Id)){
		    cout << "yes" << endl;
	    }else{
	        cout << "no"  << endl;
	    }
	}
	void Delete(int Id)
	{
		TNode* TargetNode = GetNode(Root, Id);
		assert(TargetNode->Parent != NULL);
		Erase(TargetNode);
	}
	void Print()const
	{
		InorderOutput(Root);
		cout << endl;
		PreorderOutput(Root);
		cout << endl;
	}
};

int main()
{
	int n;
	cin >> n;
	TTree Tree;
	for(int i = 0; i < n; i++){
		string Command;
		cin >> Command;
		if(Command == "print"){
			Tree.Print();
			continue;
		}
		int Id;
		cin >> Id;
		if(Command == "insert"){
			Tree.Insert(Id);
		}else if(Command == "find"){
			Tree.Find(Id);
		}else if(Command == "delete"){
			Tree.Delete(Id);
		}
	}
	return 0;
}

Measured memory_bytes: 21864