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<stdio.h>
#include<stdlib.h>

typedef struct node{
  struct node *parent;
  int key;
  struct node *left;
  struct node *right;
}Node;

Node *head;
void insert(Node *,int);
Node *make_node(Node *,int,Node *,Node *);
void inorder_print(Node *);
void preorder_print(Node *);
void free_node(Node *);
int find(Node *,int);

int main(){
  int n,key;
  char command[7];
  head = make_node(NULL,key,NULL,NULL);
  scanf("%d",&n);
  while(n--){
    scanf("%s",command);
    if(command[0] == 'i'){
     
      scanf("%d",&key);
      insert(head->left,key);
    }
    else if(command[0] == 'f'){
      scanf("%d",&key);
      if(find(head->left,key))printf("yes\n");
      else printf("no\n");
    } 
    else{
      inorder_print(head->left);
      printf("\n");
      preorder_print(head->left);
      printf("\n");
    }
  }
  free_node(head->left);
  free(head);
}

void insert(Node *node,int in_key){
  if(node == NULL)head->left = make_node(node,in_key,NULL,NULL);
  else{
    if(node->key > in_key){
      if(node->left == NULL) node->left = make_node(node,in_key,NULL,NULL);
      else insert(node->left,in_key);
    }
    else{
      if(node->right == NULL) node->right = make_node(node,in_key,NULL,NULL);
      else insert(node->right,in_key);
    }
  }
}

Node *make_node(Node *p,int k,Node *l,Node *r){
  Node *new;

  new = malloc(sizeof(Node));
  if(new == NULL){
    free(new);
    printf("node Generation failed\n");
    exit(1);
  }
  new->parent = p;
  new->key = k;
  new->left = l; 
  new->right= r;
  return new;
}
void inorder_print(Node *next){
  if(next != NULL){
    inorder_print(next->left);
    printf(" %d",next->key);
    inorder_print(next->right);
  }
}
void preorder_print(Node *next){
  if(next != NULL){
    printf(" %d",next->key);
    preorder_print(next->left);
    preorder_print(next->right);
  }
}

int find(Node *n,int k){
  if(n->key == k)return 1;
  else if(n->key > k){
    if(n->left != NULL)return find(n->left,k);
    else return 0;
  }
  else if(n->key < k){
    if(n->right != NULL)return find(n->right,k);
    else return 0;
  }
  else return 0;
}
void free_node(Node *n){
  if(n->left != NULL){
    free_node(n->left);
    free(n->left);
  }
  if(n->right != NULL){
    free_node(n->right);
    free(n->right);
  }
}