#include <assert.h>
#include <climits>
#include <iostream>
#include <string>
 
struct TNode
{
    int Value;
    TNode* Parent;
    TNode* Left;
    TNode* Right;
     
    TNode();
    TNode(int AValue, TNode* AParent);
};
 
TNode::TNode()
{
    Value  = INT_MAX;
    Parent = NULL;
    Left   = NULL;
    Right  = NULL;
}
 
TNode::TNode(int AValue, TNode* AParent)
{
    Value = AValue;
    Parent = AParent;
    Left   = NULL;
    Right  = NULL;
}
void Insert(int Key, TNode* Root);
void DeleteAll(TNode* Root)
{
    if(Root == NULL) return;
    DeleteAll(Root->Left);
    DeleteAll(Root->Right);
    delete Root;
}
 
void AddLeft(int Key, TNode* Root)
{
    assert(Root != NULL);
    if(Root->Left == NULL){
        TNode* Left = new TNode(Key, Root);
        Root->Left = Left;
    }else{
        Insert(Key, Root->Left);
    }
}
 
void AddRight(int Key, TNode* Root)
{
    assert(Root != NULL);
    if(Root->Right == NULL){
        TNode* Right = new TNode(Key, Root);
        Root->Right = Right;
    }else{
        Insert(Key, Root->Right);
    }
}
 
void Insert(int Key, TNode* Root)
{
    assert(Root != NULL);
    if(Root->Value == Key) return;
    if(Root->Value == INT_MAX){
        Root->Value = Key;
        return;
    }
    if(Root->Value > Key){
        AddLeft(Key, Root);
    }else{
        AddRight(Key, Root);
    }
}
 
TNode* Find(int Key, TNode* Node)
{
    if(Node == NULL) return NULL;
    if(Node->Value == Key) return Node;
    if(Node->Value > Key) return Find(Key, Node->Left);
    return Find(Key, Node->Right);
}

int ChildCount(const TNode* Node)
{
	int Count = 0;
	if(Node->Left) Count++;
	if(Node->Right) Count++;
	return Count;
}

TNode* GetChild(const TNode* Node)
{
	if(Node->Left == NULL) return Node->Right;
	assert(Node->Right == NULL);
	return Node->Left;
}

void EscapeFromTree(TNode* Node, TNode* Understudy)
{
	if(Understudy) Understudy->Parent = Node->Parent;
	if(Node->Parent == NULL){
		delete Node;
		return;
	}
	if(Node->Value < Node->Parent->Value){
		Node->Parent->Left = Understudy;
		delete Node;
	}else{
		Node->Parent->Right = Understudy;
		delete Node;
	}
}

TNode* LeftEnd(TNode* Node)
{
	if(Node->Left == NULL) return Node;
	return LeftEnd(Node->Left);
}

TNode* GetInOrderNext(const TNode* Node)
{
	assert(Node != NULL);
	return LeftEnd(Node->Right);
}

void Delete(TNode* Node)
{
	if(Node == NULL) return;
	if(ChildCount(Node) < 2){
		EscapeFromTree(Node, GetChild(Node));
	}else{
		TNode* Next = GetInOrderNext(Node);
		Node->Value = Next->Value;
		Delete(Next);
	}
}

void OutputPreOrder(const TNode* Node)
{
    if(Node == NULL) return;
    std::cout << " " << Node->Value;
    OutputPreOrder(Node->Left);
    OutputPreOrder(Node->Right);
}

void OutputInorder(const TNode* Node)
{
    if(Node == NULL) return;
    OutputInorder(Node->Left);
    std::cout << " " << Node->Value;
    OutputInorder(Node->Right);
}
 
void Print(const TNode* Node)
{
    OutputInorder(Node);
    std::cout << std::endl;
    OutputPreOrder(Node);
    std::cout << std::endl;
}
 
void Execute(const std::string& Command, TNode* Root)
{
    if(Command == "insert"){
        int Key = 0;
        std::cin >> Key;
        Insert(Key, Root);
        return;
    }
    if(Command == "find"){
        int Key = 0;
        std::cin >> Key;
        if(Find(Key, Root)){
            std::cout << "yes" << std::endl;
            return;
        }
        std::cout << "no" << std::endl;
    }
	if(Command == "delete"){
		int Key = 0;
		std::cin >> Key;
		Delete(Find(Key, Root));
		return;
	}
    if(Command == "print"){
        Print(Root);
    }
}
 
int main()
{
    int Count = 0;
    std::cin >> Count;
    TNode* Root = new TNode();
    for(int i = 0; i < Count; ++i){
        std::string Command;
        std::cin >> Command;
        Execute(Command, Root);
    }
    DeleteAll(Root);
    return 0;
}

Measured memory_bytes: 21756