#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)
	{
		if(Node == NULL) return;
		InorderOutput(Node->LChild);
		cout << ' ' << Node->Id;
		InorderOutput(Node->RChild);
	}
	void PreorderOutput(const TNode* Node)
	{
	    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)
	{
		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);
		}
	}
	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){
			if(Node->Parent->LChild == Node){
				Node->Parent->LChild = Node->LChild;
				Node->LChild->Parent = Node->Parent;
			}else{
				Node->Parent->RChild = Node->LChild;
				Node->LChild->Parent = Node->Parent;
			}
			delete Node;
		}else if(Node->LChild == NULL && Node->RChild != NULL){
			if(Node->Parent->LChild == Node){
				Node->Parent->LChild = Node->RChild;
				Node->RChild->Parent = Node->Parent;
			}else{
				Node->Parent->RChild = Node->RChild;
				Node->RChild->Parent = Node->Parent;
			}
			delete Node;
		}else{
			assert(Node->RChild != NULL);
			TNode* InorderNextNode = GetInorderNextNode(Node->RChild);
			Node->Id = InorderNextNode->Id;
			Erase(InorderNextNode);
		}
	}
public:
	TTree()
	{
		Root = NULL;
	}
	~TTree()
	{
		Delete(Root);
	}
	void Insert(int Id)
	{
		Insert(new TNode(Id));
	}
	void Find(int Id)
	{
	    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()
	{
		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 == "insert"){
			int Id;
			cin >> Id;
			Tree.Insert(Id);
		}else if(Command == "find"){
			int Id;
			cin >> Id;
			Tree.Find(Id);
		}else if(Command == "delete"){
			int Id;
			cin >> Id;
			Tree.Delete(Id);
		}else{
			Tree.Print();
		}
	}
	return 0;
}

Measured memory_bytes: 21904