Problem:
Binary Search Tree II

Write a program which performs the following operations to a binary search tree $T$ by adding the find operation to A: Binary Search Tree I.

insert $k$: Insert a node containing $k$ as key into $T$.

find $k$: Report whether $T$ has a node containing $k$.

print: Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively.

Input

In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by insert $k$, find $k$ or print are given.

Output

For each find $k$ operation, print "yes" if $T$ has a node containing $k$, "no" if not.

In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key.

Constraints

The number of operations $\leq 500,000$

The number of print operations $\leq 10$.

$-2,000,000,000 \leq key \leq 2,000,000,000$

The height of the binary tree does not exceed 100 if you employ the above pseudo code.

The keys in the binary search tree are all different.

Sample Input 1

10
insert 30
insert 88
insert 12
insert 1
insert 20
find 12
insert 17
insert 25
find 16
print

Sample Output 1

yes
no
1 12 17 20 25 30 88
30 12 1 20 17 25 88

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in C++14 (judge verdict: Accepted)

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <clocale>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i)
#define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i)
#define VFOR(i, s, n) for(int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for(int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i)
#define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ll = long long;
using Pi_i = pair<int, int>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;

const int MOD = 1000000007;
const int INF = 1000000000;
const int NIL = -1;

template<class T>
bool chmax(T &a, const T &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T>
bool chmin(T &a, const T &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}




class BinarySearchTree{
    struct Node{
        int key;
        Node *right, *parent, *left;
        Node(): key(NIL), right(nullptr), parent(nullptr), left(nullptr){};
        Node(int k): key(k), right(nullptr), parent(nullptr), left(nullptr){};
    };
    Node *root, *NILL = nullptr;
public:
    BinarySearchTree(): root(nullptr){};

    void insert(int k){
        Node *y = nullptr;
        Node *x = root;
        Node *z = new Node(k);
        while(x != nullptr){
            y = x;
            if(z->key < x->key){
                x = x->left;
            }else{
                x = x->right;
            }
        }
        z->parent = y;
        if(y == nullptr)
            root = z;
        else{
            if(z->key < y->key){
                y->left = z;
            }else{
                y->right = z;
            }
        }
    }

private:
    Node* find(Node *v, int k){
        while(v != nullptr && k != v->key){
            if(k < v->key) v = v->left;
            else v = v->right;
        }
        return v;
    }

    Node* treeMinimum(Node *v){
        while(v->left != nullptr)
            v = v->left;
        return v;
    }

    Node* treeSuccesor(Node *v){
        if(v->right != nullptr) return treeMinimum(v->right);
        Node *y = v->parent;
        while(y != nullptr && v == y->right){
            v = y;
            y = y->parent;
        }
        return y;
    }

    void tDelete(Node *z){
        Node *y, *x;
        if(z->left == nullptr || z->right == nullptr)
            y = z;
        else y = treeSuccesor(z);
        if(y->left != nullptr){
            x = y->left;
        }else{
            x = y->right;
        }
        if(x != nullptr)
            x->parent = y->parent;
        if(y->parent == nullptr)
            root = x;
        else{
            if(y == y->parent->left)
                y->parent->left = x;
            else y->parent->right = x;
        }
        if(y != z){
            z->key = y->key;
        }
        delete(y);
    }

    void inOrder(Node *v){
        if(v == nullptr) return;
        inOrder(v->left);
        cout << " " << v->key;
        inOrder(v->right);
    }

    void preOrder(Node *v){
        if(v == nullptr) return;
        cout << " " << v->key;
        preOrder(v->left);
        preOrder(v->right);
    }
    
public:
    bool find(int x){
        Node *t = find(root, x);
        return t != nullptr;
    }

    void tDelete(int x){
        tDelete(find(root, x));
    }

    void outInOrder(){
        inOrder(root);
    }

    void outPreOrder(){
        preOrder(root);
    }

};


int main(){
    int n; cin >> n;
    BinarySearchTree T;
    REP(i, n){
        int x;
        string com; cin >> com;
        if(com == "insert"){
            cin >> x;
            T.insert(x);
        }
        else if(com == "find"){
            cin >> x;
            cout << (T.find(x) ? "yes" : "no") << endl;
        }
        else if(com == "delete"){
            cin >> x;
            T.tDelete(x);
        }
        else{
            T.outInOrder();
            cout << "\n";
            T.outPreOrder();
            cout << "\n";
        }
    }
    return 0;
}