#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <algorithm>
 


template<typename T>
struct TNode
{
    T Value;
    TNode* Parent;
    TNode* Left;
    TNode* Right;

    TNode(const T& AValue)
     : Value(AValue), Parent(NULL), Left(NULL), Right(NULL)
    {
    }
    // T Valueconst
    // {
    //     return FValue;
    // }
    // void SetValue(const T& Value)
    // {
    //     FValue = Value;
    // }
    // void AddNode(TNode* Node)
    // {
    //     Node->FParent = this;
    //     if(FValue < Node->FValue){
    //         assert(FRight);
    //         FRight = Node;
    //     }else{
    //         assert(FLeft);
    //         FLeft  = Node;
    //     }
    // }
    // void SetLeft(TNode* Node)
    // {
    //     if(Node) Node->FParent = this;
    //     FLeft = Node;
    // }
    // void SetRight(TNode* Node)
    // {
    //     if(Node) Node->FParent = this;
    //     FRight = Node;
    // }
    // void SetParent(TNode* Node)
    // {
    //     FParent = Node;
    // }
    // TNode* CreateLeft(const T& Value)
    // {
    //     assert(FLeft == NULL);
    //     SetLeft(new TNode(Value));
    //     return FLeft;
    // }
    // TNode* CreateRight(const T& Value)
    // {
    //     assert(FRight == NULL);
    //     SetRight(new TNode(Value));
    //     return FRight;
    // }
    // const TNode* Parent()const
    // {
    //     return FParent;
    // }
    // TNode* Parent()
    // {
    //     return FParent;
    // }
    // const TNode* Leftconst
    // {
    //     return FLeft;
    // }
    // TNode* Left
    // {
    //     return FLeft;
    // }
    // const TNode* Rightconst
    // {
    //     return FRight;
    // }
    // TNode* Right
    // {
    //     return FRight;
    // }
    // const TNode* Find(const T& Value)const
    // {
    //     // std::cout << "Find(" << Value << ")" << std::endl;
    //     if(FValue == Value) return this;
    //     TNode* Node = FValue < Value ? FRight : FLeft;
    //     if(Node) return Node->Find(Value);
    //     return NULL;
    // }
    // TNode* Find(const T& Value)
    // {
    //     return const_cast<TNode*>(const_cast<const TNode*>(this)->Find(Value));
    // }
};

template<typename T>
void PrintPreorder(const TNode<T>* Node)
{
//  0 1 2 3 4 5 6 7 8
    if(Node == NULL) return;
    std::cout << " " << Node->Value;
    PrintPreorder(Node->Left);
    PrintPreorder(Node->Right);
}
template<typename T>
void PrintInorder(const TNode<T>* Node)
{
//  2 1 3 0 6 5 7 4 8
    if(Node == NULL) return;
    PrintInorder(Node->Left);
    std::cout << " " << Node->Value;
    PrintInorder(Node->Right);
}
template<typename T, typename TIt>
void AddNode(TNode<T>* Node, TIt& PreBegin, TIt PreEnd, TIt InBegin, TIt InEnd);

template<typename T, typename TIt>
void AddLeftNode(TNode<T>* Node, TIt& PreBegin, TIt PreEnd, TIt InBegin, TIt InEnd)
{
    if(PreBegin == PreEnd) return;
    if(InBegin == InEnd) return;
    TNode<T>* New = new TNode<T>(*PreBegin);
    Node->SetLeft(New);
    AddNode(New, ++PreBegin, PreEnd, InBegin, InEnd);
}

template<typename T, typename TIt>
void AddRightNode(TNode<T>* Node, TIt& PreBegin, TIt PreEnd, TIt InBegin, TIt InEnd)
{
    if(PreBegin == PreEnd) return;
    if(InBegin == InEnd) return;
    TNode<T>* New = new TNode<T>(*PreBegin);
    Node->SetRight(New);
    AddNode(New, ++PreBegin, PreEnd, InBegin, InEnd);
}

template<typename T, typename TIt>
void AddNode(TNode<T>* Node, TIt& PreBegin, TIt PreEnd, TIt InBegin, TIt InEnd)
{
    if(PreBegin == PreEnd)  return;
    TIt It = std::find(InBegin, InEnd, Node->Value);
    assert(It != InEnd);
    AddLeftNode(Node, PreBegin, PreEnd, InBegin, It);
    AddRightNode(Node, PreBegin, PreEnd, It+1, InEnd);
}

template<typename T>
class TBinaryTree
{
    TNode<T>* FRoot;
    
    TNode<T>* Find(TNode<T>* Node, const T& Value)
    {
        if(Node->Value == Value) return Node;
        Node = Node->Value < Value ? Node->Right : Node->Left;
        return Node ? Find(Node, Value) : NULL;
    }
    TNode<T>* NextInOrderNode(TNode<T>* Node)
    {
        if(Node->Right == NULL) return Node->Parent;
        Node = Node->Right;
        while(Node->Left) Node = Node->Left;
        return Node;
    }
    void Delete(TNode<T>* Node)
    {
        if(Node == FRoot){
            delete FRoot;
            FRoot = NULL;
            return;
        }
        if(Node->Left  == NULL
        && Node->Right == NULL){
            if(Node->Parent->Left  == Node) Node->Parent->Left  = NULL;
            if(Node->Parent->Right == Node) Node->Parent->Right = NULL;
            delete Node;
        }else
        if(Node->Left && Node->Right){
            TNode<T>* Next = NextInOrderNode(Node);
            Node->Value = Next->Value;
            Delete(Next);
        }else{
            TNode<T>* Child = Node->Left ? Node->Left : Node->Right;
            Child->Parent = Node->Parent;
            if(Node->Parent->Left  == Node) Node->Parent->Left  = Child;
            if(Node->Parent->Right == Node) Node->Parent->Right = Child;
            delete Node;
        }
    }
public:
    TBinaryTree()
    {
        FRoot = NULL;
    }
    void Insert(const T& Value)
    {
        TNode<T>* New = new TNode<T>(Value);
        TNode<T>* Parent = NULL;
        TNode<T>* Node = FRoot;
        while(Node){
            Parent = Node;
            if(New->Value < Node->Value){
                Node = Node->Left;
            }else{
                Node = Node->Right;
            }
        }
        New->Parent = Parent;
        if(Parent == NULL){
            FRoot = New;
        }else if(New->Value < Parent->Value){
            Parent->Left = New;
        }else{
            Parent->Right = New;
        }
    }
    bool Find(const T& Value)
    {
        if(FRoot == NULL) return false;
        return Find(FRoot, Value);
    }
    void Delete(const T& Value)
    {
        if(FRoot == NULL) return;
        TNode<T>* Target = Find(FRoot, Value);
        if(Target == NULL) return;

        Delete(Target);
        
    }
    void Print()
    {
        PrintInorder(FRoot);
        std::cout << std::endl;
        PrintPreorder(FRoot);
        std::cout << std::endl;
    }
};

int main()
{
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    int n;
    std::cin >> n;
    TBinaryTree<int> Tree;
    for(std::size_t i = 0; i < n; ++i){
        std::string Command;
        std::cin >> Command;
        if(Command[0] == 'i'){
            int Value;
            std::cin >> Value;
            Tree.Insert(Value);
        }else 
        if(Command[0] == 'f'){
            int Value;
            std::cin >> Value;
            std::cout << (Tree.Find(Value) ? "yes" : "no") << std::endl;
        }else
        if(Command[0] == 'd'){
            int Value;
            std::cin >> Value;
            Tree.Delete(Value);
        }else{
            Tree.Print();
        }
    }
    return 0;
}

Measured memory_bytes: 21872