#include <stdio.h>
#include <vector>
#include <list>
#include <map>
#include <array>
#include <memory>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <stdexcept>


using std::string;
using std::vector;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// util
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::vector<std::string> split(const std::string& input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter)) {
        result.push_back(field);
    }
    return result;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// user func
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void printList(const vector<int>& list) {
    // print
    for(int i = 0; i < list.size(); i++){
        printf(" %d", list[i]);
    }
    printf("\n");
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// logic
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Node;

class Node{
public:
    Node(int value)
        :value(value)
    {
    }
    void Value(int value) {
        this->value = value;
    }
    int Value(){
        return this->value;
    }
    std::shared_ptr<Node> Left(){
        return this->left;
    }
    void Left(std::shared_ptr<Node> left){
        this->left = left;
    }
    std::shared_ptr<Node> Right(){
        return this->right;
    }
    void Right(std::shared_ptr<Node> right){
        this->right = right;
    }
    bool HasLeft(){
        return this->left ? true : false;
    }
    bool HasRight(){
        return this->right ? true : false;
    }
    std::shared_ptr<Node> Parent(){
        return this->parent;
    }
    void Parent(std::shared_ptr<Node> parent){
        this->parent = parent;
    }
    int CountChild(){
        int count = 0;
        if (this->HasLeft()) count++;
        if (this->HasRight()) count++;
        return count;
    }

    std::shared_ptr<Node> FirstChild() {
        if(this->HasLeft()) return this->Left();
        if(this->HasRight()) return this->Right();
        return nullptr;
    }

    bool IsEqualLeft(int val){
        if (!this->HasLeft()) return false;
        return this->Left()->Value() == val;
    }

private:
    int value;
    std::shared_ptr<Node> left = nullptr ;
    std::shared_ptr<Node> right = nullptr;
    std::shared_ptr<Node> parent = nullptr;
};

using NodePtr = std::shared_ptr<Node>;

class BinarySearchTree{
public:
    void Insert(int value){
        if(!this->root){
            root = std::make_shared<Node>(value);
            return;
        }
        InsertProc(this->root, value);
    }
    
    bool Find(int value){
        if(!this->root) return false;
        return this->FindProc(this->root, value);
    }
    void Delete(int value){
        NodePtr target = this->FindNode(this->root, value);
        // printf("Find:%d\n", target->Value());
        // printf("parent:%d\n", target->Parent()->Value());
        // if(target->HasLeft()) printf("left:%d\n", target->Left()->Value());
        // if(target->HasRight()) printf("right:%d\n", target->Right()->Value());
        this->DeleteProc(target);
    }
    vector<int> Preorder(){
        vector<int> result;
        this->PreorderProc(this->root, result);
        return result;
    }
    vector<int> Inorder(){
        vector<int> result;
        this->InorderProc(this->root, result);
        return result;
    }
private:
    NodePtr root = nullptr;

    void InsertProc(NodePtr node, int value){
        if(node->Value() < value){
            if(node->HasRight()){
                InsertProc(node->Right(), value);
            }else{
                node->Right(std::make_shared<Node>(value));
                node->Right()->Parent(node);
            }
        }else{
            if(node->HasLeft()){
                InsertProc(node->Left(), value);
            }else{
                node->Left(std::make_shared<Node>(value));
                node->Left()->Parent(node);
            }
        }
    }

    void PreorderProc(NodePtr current, vector<int>& list){
        list.push_back(current->Value());
        if (current->HasLeft()) PreorderProc(current->Left(), list);
        if (current->HasRight()) PreorderProc(current->Right(), list);
    }
    
    void InorderProc(NodePtr current, vector<int>& list){
        if (current->HasLeft()){
            InorderProc(current->Left(), list);
        }
        list.push_back(current->Value());
        if (current->HasRight()) {
            InorderProc(current->Right(), list);
        }
    }

    bool FindProc(NodePtr current, int value){
        if (current->Value() == value){
            return true;
        } else if (value < current->Value()) {
            if(!current->HasLeft()) return false;
            return this->FindProc(current->Left(), value);
        } else {
        // if (current->Value() < value) {
            if(!current->HasRight()) return false;
            return this->FindProc(current->Right(), value);
        }
    }

    NodePtr FindNode(NodePtr current, int value){
        if (current->Value() == value){
            return current;
        } else if (value < current->Value()) {
            if(!current->HasLeft()) return nullptr;
            return this->FindNode(current->Left(), value);
        } else {
        // if (current->Value() < value) {
            if(!current->HasRight()) return nullptr;
            return this->FindNode(current->Right(), value);
        }
    }
    // inorder s next
    NodePtr FindNext(NodePtr node){
        if (node->HasLeft()) return FindNext(node->Left());
        return node;
    }
    void dump(NodePtr node){
        printf("%d %d(%d, %d)\n", 
            node->Value(),
            node->Parent() ? node->Parent()->Value() : - 1, 
            node->Left() ? node->Left()->Value() : -1,
            node->Right() ? node->Right()->Value() : -1);
        if (node->HasLeft()) dump(node->Left());
        if (node->HasRight()) dump(node->Right());
    }
    void DeleteProc(NodePtr current){
        // dump(root);

        // remove current
        if (current->CountChild() == 0) {
            if (current->Parent()->IsEqualLeft(current->Value())){
                current->Parent()->Left(nullptr);
            }else{
                current->Parent()->Right(nullptr);
            }
            return;
        }
        // remove
        if (current->CountChild() == 1) {
            if (current->Parent()->IsEqualLeft(current->Value())){
                current->Parent()->Left(current->FirstChild());
            }else{
                current->Parent()->Right(current->FirstChild());
            }
            current->FirstChild()->Parent(current->Parent());
            return;
        }
        //  if (current->CountChild() == 2) {
        NodePtr next = this->FindNext(current->Right());
        // printf("cur: %d  next:%d\n", current->Value(), next->Value());
        current->Value(next->Value());
        this->DeleteProc(next);

    }
};

void solve() {
    // binary trees
    int n;
    scanf("%d", &n);
    BinarySearchTree tree;
    for(int i = 0; i < n; i++){
        char buffer[127];
        scanf("%s", buffer);
        string command(buffer);
        if(command == "insert"){
            int v;
            scanf(" %d", &v);
            tree.Insert(v);
        } else if(command == "find" ){
            int v;
            scanf(" %d", &v);
            printf("%s\n", tree.Find(v) ? "yes" : "no");
        } else if(command == "delete") {
            int v;
            scanf(" %d", &v);
            tree.Delete(v);
        
        } else if(command == "print"){
            auto in = tree.Inorder();
            printList(in);
            auto pre = tree.Preorder();
            printList(pre);
        }

    }
        
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
    solve();
    return 0;
}


Measured memory_bytes: 38896