template<typename T>
class NodeAllocator {
public:
    NodeAllocator() = default;
    explicit NodeAllocator(int num): reserved_(num), assigned_(0) {
        buf_ = new T[num];
    };

    T* GetAlloc(int num) {
        if (assigned_ + num >= reserved_) {
            return nullptr;
        }
        int retp = assigned_;
        assigned_ += num;
        return buf_ + retp;
    }

    void ReAlloc(int num){
        DeleteAllAlloc();
        buf_ = new T[num];
        reserved_ = num;
    }

    ~NodeAllocator() {
        DeleteAllAlloc();
    }

//    void DeleteElem(T* e) {
//        return;
//    }

private:
    void DeleteAllAlloc() {
        if (reserved_ == 0) {
            return;
        }
        if (reserved_ > 1) {
            delete [] buf_;
        }else{
            delete buf_;
        }

        assigned_ = 0;
        reserved_ = 0;
    }

    int reserved_;
    int assigned_;
    T *buf_;
};

#include <iostream>
#include <vector>


struct Node {
    int key;
    Node *p = nullptr;
    Node *l = nullptr;
    Node *r = nullptr;
};

NodeAllocator<Node> Alloc;

Node *root;

void Insert(int v) {
    Node *p = nullptr;
    Node *x = root;
    Node *k = Alloc.GetAlloc(1);
    k->key = v;
    while (x != nullptr) {
        p = x;
        if (k->key < x->key) {
            x = x->l;
        } else {
            x = x->r;
        }
    }
    k->p = p;
    if (p == nullptr) {
        root = k;
    } else if (k->key < p->key) {
        p->l = k;
    } else {
        p->r = k;
    }
}

Node *Find(Node *r, int key) {
    while (r != nullptr && r->key != key) {
        if (key < r->key) {
            r = r->l;
        } else {
            r = r->r;
        }
    }
    return r;
}

Node *GetMinimum(Node *r) {
    while (r->l != nullptr) {
        r = r->l;
    }
    return r;
}

Node *GetSuccessor(Node *r) {
    if (r->r != nullptr) {
        return GetMinimum(r->r);
    }
    Node* p = r->p;
    while ((p != nullptr) && (r == p->r)) {
        r = p;
        p = p->p;
    }
    return p;
}

bool Delete(Node *r, Node *tgt) {
    if (tgt == nullptr) {
        return false;
    }
    // Having no children
    if ((tgt->l == nullptr) && (tgt->r == nullptr)) {
        if (tgt->key < tgt->p->key) {
            tgt->p->l = nullptr;
        } else {
            tgt->p->r = nullptr;
        }
        tgt->key = 0;
        tgt->p = nullptr;
        tgt->l = nullptr;
        tgt->r = nullptr;
    }
        //Having two children.
    else if ((tgt->l != nullptr) && (tgt->r != nullptr)) {
        Node* next = GetSuccessor(tgt);
//        Node *next = tgt->r;
//        while (next->l != nullptr) {
//            next = next->l;
//        }
        tgt->key = next->key;
        Delete(r, next);
    }
        //Having one child.
    else {
        Node *c = nullptr;
        if (tgt->l != nullptr) {
            c = tgt->l;
        } else {
            c = tgt->r;
        }
        c->p = tgt->p;
        if (tgt->key < tgt->p->key) {
            tgt->p->l = c;
        } else {
            tgt->p->r = c;
        }
        tgt->key = 0;
        tgt->p = nullptr;
        tgt->l = nullptr;
        tgt->r = nullptr;
    }
    return true;
}


void Preorder(Node *tgt) {
    if (tgt == nullptr) {
        return;
    }
    std::cout << " " << tgt->key;
    Preorder(tgt->l);
    Preorder(tgt->r);
}

void Inorder(Node *tgt) {
    if (tgt == nullptr) {
        return;
    }
    Inorder(tgt->l);
    std::cout << " " << tgt->key;
    Inorder(tgt->r);
}

int main() {
    int n;
    std::cin >> n;
    Alloc.ReAlloc(n);
    for (int i = 0; i < n; i++) {
        std::string str;
        std::cin >> str;
        if (str == "print") {
            Inorder(root);
            std::cout << std::endl;
            Preorder(root);
            std::cout << std::endl;
            continue;
        }
        int k;
        std::cin >> k;
        if (str == "insert") {
            Insert(k);
        } else if (str == "find") {
            if (Find(root, k) != nullptr) {
                std::cout << "yes" << std::endl;
            } else {
                std::cout << "no" << std::endl;
            }
        } else if (str == "delete") {
            Node *tgt = Find(root, k);
            Delete(root, tgt);
        }
    }
}


Measured memory_bytes: 18268