#include <cstdio>
#include <cstdint>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <memory>
#include <unordered_map>

struct Node {
public:
    Node(int key_in) :
        parent(nullptr),
        left(nullptr),
        right(nullptr),
        key(key_in)
    {}

public:
    Node* parent;
    Node* left;
    Node* right;
    int   key;
};

struct Tree {
public:
    Tree() :
        root(nullptr)
    {}

    Node* root;
};

void insert(Tree& t, int key)
{
    Node* z = new Node(key);

    Node* y = nullptr;
    Node* x = t.root;
    while (x != nullptr) {
        y = x;
        if (z->key < x->key) {
            x = x->left;
        } else {
            x = x->right;
        }
    }

    z->parent = y;

    if (y == nullptr) {
        t.root = z;
    } else if (z->key < y->key) {
        y->left = z;
    } else {
        y->right = z;
    }
}

Node* getSuccessor(Node* node)
{
    if (node->right != nullptr) {
        Node* current = node->right;
        while (current->left != nullptr) {
            current = current->left;
        }

        return current;
    }
}

void deleteNode(Node* node, int key)
{
    if (node->left == nullptr && node->right == nullptr) {
        if (node == node->parent->left) {
            node->parent->left = nullptr;
            delete(node);
        } else {
            node->parent->right = nullptr;
            delete(node);
        }
    } else if (node->left != nullptr && node->right == nullptr) {
        node->left->parent = node->parent;
        if (node == node->parent->left) {
            node->parent->left = node->left;
            delete(node);
        } else {
            node->parent->right = node->left;
            delete(node);
        }
    } else if (node->left == nullptr && node->right != nullptr) {
        node->right->parent = node->parent;
        if (node == node->parent->left) {
            node->parent->left = node->right;
            delete(node);
        } else {
            node->parent->right = node->right;
            delete(node);
        }
    }
}

void del(Tree& t, int key)
{
    Node* node = t.root;
    if (node == nullptr) {
        return;
    }

    while (node != nullptr) {
        if (node->key == key) {
            if (node->parent == nullptr) {
                t.root = nullptr;
                delete(node);
                break;
            } else if (node->left != nullptr && node->right != nullptr) {
                Node* successor = getSuccessor(node);
                node->key = successor->key;
                deleteNode(successor, key);
                break;
            } else {
                deleteNode(node, key);
                break;
            }
        } else if (node->key < key) {
            node = node->right;
        } else {
            node = node->left;
        }
    }
}

void printInOrder(Node* node)
{
    if (node->left != nullptr) {
        printInOrder(node->left);
    }

    std::printf(" %d", node->key);

    if (node->right != nullptr) {
        printInOrder(node->right);
    }
}

void printPreorder(Node* node)
{
    std::printf(" %d", node->key);

    if (node->left != nullptr) {
        printPreorder(node->left);
    }

    if (node->right != nullptr) {
        printPreorder(node->right);
    }
}

void print(Tree& t)
{
    Node* root = t.root;
    if (root == nullptr) {
        return;
    }

    printInOrder(root);
    std::cout << std::endl;
    printPreorder(root);
    std::cout << std::endl;
}

void find(Tree& t, int key)
{
    Node* node = t.root;
    while (node != nullptr) {
        if (node->key == key) {
            std::cout << "yes" << std::endl;
            return;
        } else if (node->key < key) {
            node = node->right;
        } else {
            node = node->left ;
        }
    }

    std::cout << "no" << std::endl;
}

int main()
{
    Tree t;

    int n;
    std::cin >> n;

    std::string str;
    int key;
    for (int i = 0; i < n; ++i) {
        std::cin >> str;
        if (str == "insert") {
            std::cin >> key;
            insert(t, key);
        } else if (str == "find") {
            std::cin >> key;
            find(t, key);
        } else if (str == "delete") {
            std::cin >> key;
            del(t, key);
        } else if (str == "print") {
            print(t);
        } else {
            return -1;
        }
    }

    return 0;
}


Measured memory_bytes: 21968