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++ (judge verdict: Accepted)

#include<iostream>

template<class T>
struct binary_serch_tree{
  struct tree{
    T id;
    tree* left;
    tree* right;
    tree* parent;
  };
  tree* root;
  tree original;
  tree* null=&original;
  binary_serch_tree(){
    this->root=this->null;
    root->right=this->null;
    root->left=this->null;
  }

  void set_tree(tree* tr,tree* root){
    tree* null=this->null;
    tree* left=root->left;
    tree* right=root->right;
    if(root->id>tr->id){
      if(left==null){
        root->left=tr;
        tr->parent=root;
        tr->left=null;
        tr->right=null;
      }else{
        set_tree(tr,left);
      }
    }else{
      if(right==null){
        root->right=tr;
        tr->parent=root;
        tr->left=null;
        tr->right=null;
      }else{
        set_tree(tr,right);
      }
    }
  }

  void insert(T a){
    tree* tr=new tree;
    tr->id=a;
    if(this->root==this->null){
      this->root=tr;
      tr->left=this->null;
      tr->right=this->null;
      tr->parent=this->null;
    }else{
      set_tree(tr,this->root);
    }
  }
  void go_pre(tree* root){
    std::cout<<" "<<root->id;
    tree* left=root->left;
    tree* right=root->right;
    tree* null=this->null;
    if(left==null&&right==null){
      //do nothing
    }else if(left==null){
      go_pre(right);
    }else if(right==null){
      go_pre(left);
    }else{
      go_pre(left);
      go_pre(right);
    }
  }
  void print_pre(){
    go_pre(this->root);
  }
  void go_in(tree *root){
    tree* left=root->left;
    tree* right=root->right;
    tree* null=this->null;
    if(left==null&&right==null){
      std::cout<<" "<<root->id;
    }else if(left==null){
      std::cout<<" "<<root->id;
      go_in(right);
    }else if(right==null){
      go_in(left);
      std::cout<<" "<<root->id;
    }else{
      go_in(left);
      std::cout<<" "<<root->id;
      go_in(right);
    }
  }
  void print_in(){
    go_in(this->root);
  }

  int check_num(T a,int* check,tree* root){
    if(root->id==a){
      *check=1;
      return 0;
    }else{
      if(root->id>a&&root->left!=this->null){
        check_num(a,check,root->left);
      }else if(root->id<a&&root->right!=this->null){
        check_num(a,check,root->right);
      }else{
        return 0;
      }
    }
    return 0;
  }

  void find(T a){
    int* check;
    int c=0;
    check=&c;
    check_num(a,check,this->root);
    if(*check==0){
      std::cout<<"no"<<std::endl;
    }else{
      std::cout<<"yes"<<std::endl;
    }
  }

};



int main(){
  int n;
  std::cin>>n;
  binary_serch_tree<int> T;
  for(int i=0;i<n;i++){
    std::string s;
    std::cin>>s;
    if(s=="insert"){
      int a;
      std::cin>>a;
      T.insert(a);
    }else if(s=="find"){
      int a;
      std::cin>>a;
      T.find(a);
    }else{
      T.print_in();
      std::cout<<std::endl;
      T.print_pre();
      std::cout<<std::endl;
    }
  }
}