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

typedef struct Node Node;

struct Node{
    int data;
    Node *parent;
    Node *left;
    Node *right;
};

void initNode(Node*);
Node copyNode(Node*,Node*);
void insert(Node*,Node*[],int);
void printTree(Node*);
int find(Node*,int);
void inorder(Node*);
void preorder(Node*);


int main(int argc, char** argv) {
    int commandSize;
    scanf("%d",&commandSize);
    Node *tree[commandSize-1];
    for(int i = 0; i < commandSize; i++)
        tree[i] = NULL;
    int loc = 0;
    int j;
    int findVal;
    Node temp[commandSize-1];
    char cmd[7];
    for(int i = 0; i < commandSize; i++){
        initNode(&temp[i]);
        scanf("%s",cmd);
        if(!strcmp(cmd,"insert")){
            scanf("%d", &temp[i].data);
            insert(&temp[i],tree,loc++);
        }
        else if(!strcmp(cmd,"print")){
            if(tree[0]==NULL){
                j = 0;
                while(j < 4){
                    if(tree[j]==NULL)
                        tree[j] = &temp[j];
                    j++;
                }
            }
            printTree(tree[0]);
        }
        else{
            scanf("%d",&findVal);
            if(find(tree[0],findVal))
                printf("yes\n");
            else
                printf("no\n");
        }
    }
    return (EXIT_SUCCESS);
}

void initNode(Node *n){
    n->data = NULL;
    n->parent = NULL;
    n->left = NULL;
    n->right = NULL;
}

void insert(Node *node, Node *tree[], int loc){
    Node *parent = NULL;
    Node *current = tree[0];
    while(current != NULL){
        parent = current;
        if(node->data < current->data)
            current = current->left;
        else
            current = current->right;
    }
    node->parent = parent;
    if(parent == NULL){
        tree[loc] = node;
    }
    else if(node->data < parent->data){
        tree[loc] = node;
        parent->left = tree[loc];
    }
    else{
        tree[loc] = node;
        parent->right = tree[loc];
    }
}

Node copyNode(Node *dest, Node *orig){
    dest->data = orig->data;
    dest->parent = orig->parent;
    dest->left = orig->left;
    dest->right = orig->right;
}

void printTree(Node *root){
    inorder(root);
    printf("\n");
    preorder(root);
    printf("\n");
}

int find(Node *root, int val){
    if(root != NULL){
        if(val < root->data)
            return find(root->left,val);
        else if(val > root->data)
            return find(root->right,val);
        else if(val == root->data)
            return 1;
    }
    else
        return 0;
}

void inorder(Node *root){
    if(root->left != NULL)
        inorder(root->left);
    printf(" %d", root->data);
    if(root->right != NULL)
        inorder(root->right);
}

void preorder(Node *root){
    printf(" %d", root->data);
    if(root->left != NULL)
        preorder(root->left);
    if(root->right != NULL)
        preorder(root->right);
}