#include <iostream>
#include <string>
#include <memory>
using namespace std;

class Set
{
private:

    struct Node_t;
    typedef shared_ptr<Node_t> Node;
    
    struct Node_t
    {
        Node_t(Node par_, Node left_, Node right_, int val) :
            par(par_), left(left_), right(right_), key(val){}
        Node par, left, right;
        int key;
    };
    
    void preorder(const Node& x){
        cout << ' ' << x->key;
        if(x->left != nullptr){
            preorder(x->left);
        }
        if(x->right != nullptr){
            preorder(x->right);
        }
    }
    
    void inorder(const Node& x){
        if(x->left != nullptr){
            inorder(x->left);
        }
        cout << ' ' << x->key;
        if(x->right != nullptr){
            inorder(x->right);
        }
    }
    
    
public:
    Set():size(0){}
    void insert(int val){
        if(size == 0){
            root = make_shared<Node_t>(nullptr, nullptr, nullptr, val);
        } else {
            Node y = nullptr;
            Node x = root;
            while(x != nullptr){
                y = x;
                if(val < x->key){
                    x = x->left;
                } else {
                    x = x->right;
                }
            }
            
            if(val < (y->key)){
               y->left = make_shared<Node_t>(y,nullptr,nullptr,val);
            } else {
               y->right = make_shared<Node_t>(y,nullptr,nullptr,val);
            }
        }
        ++size;
    }

    bool find(int val){
        if(size == 0) return false;
        Node x = root;
        while(x != nullptr){
            if(x->key == val){
                return true;
            }
            if(val < x->key){
                x = x->left;
            } else {
                x = x->right;
            }
        }
        return false;
    }
    
    Node findNode(int val){
        if(size == 0) return nullptr;
        Node x = root;
        while(x != nullptr){
            if(x->key == val){
                return x;
            }
            if(val < x->key){
                x = x->left;
            } else {
                x = x->right;
            }
        }
        return nullptr;
    }
    
    void deleteNode(int k){
        Node z = findNode(k);
        if(z == nullptr) return;
        
        Node x,y;
        if(z->left == nullptr || z->right == nullptr){
            y = z;
        } else {
            y = successor(z);
        }
        
        if(y->left != nullptr){
            x = y->left;
        } else {
            x = y->right;
        }
        
        if(x != nullptr){
            x->par = y->par;
        }
        
        if(y->par == nullptr){
            root = x;
        } else if(y == y->par->left){
            y->par->left = x;
        } else {
            y->par->right = x;
        }
        
        if(y != z){
            z->key = y->key;
        }
        --size;
    }
    
    Node successor(Node x){
        if(x->right != nullptr){
            return min(x->right);
        }
        Node y = x->par;
        while(y != nullptr && x == y->right){
            x = y;
            y = y->par;
        }
        return y;
    }

    Node min(Node x){
        while(x->left != nullptr){
            x = x->left;
        }
        return x;
    }

    void print(){
        if(size == 0) return;
        inorder(root);
        cout << endl;
        preorder(root);
        cout << endl;
    }
    
private:
    Node root;
    int size;
};

int main()
{
    ios_base::sync_with_stdio(false);
    int n, k;
    string s;
    Set set;
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> s;
        if(s == "insert"){
           cin >> k;
           set.insert(k);
        } else if(s == "find"){
            cin >> k;
            cout << (set.find(k) ? "yes" : "no") << endl;
        } else if(s == "delete"){
            cin >> k;
            set.deleteNode(k);
        } else if(s == "print"){
            set.print();
        }
    }
}

Measured memory_bytes: 38732