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)

#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define ABS(x) ( (x)>0 ? x : -(x) )
#define MIN(x,y) ( (x) < (y) ? (x) : (y) )
#define MAX(x,y) ( (x) > (y) ? (x) : (y) )

#define MOD 1000000007

void swap(int* a, int* b)
{
	int tmp;
	tmp = *a;
	*a = *b;
	*b = tmp;
}

void print(int A[], int n)
{
	int i;
	for (i = 0; i < n; i++)
	{
		printf("%d", A[i]);
		if (i != n - 1)
			printf(" ");
	}
	printf("\n");
}

int gcd(int a, int b)
{
	if (a < b)
		swap(&a, &b);

	if (a % b == 0)
		return b;
	else
		return gcd(b, a % b);
}

int lcm(int a, int b)
{
	return a * b / gcd(a, b);
}

long long modpow(long long a, long long n, long long mod) {
	long long res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
		printf("res:%lld a:%lld\n", res,a);
	}
	return res;
}

typedef struct node
{
	int data;
	struct node* left, * right;
}NODE;

NODE* root = NULL;

void insert(int key)
{
	NODE** p, * new;

	p = &root;

	while (*p != NULL)
	{
		if (key == (*p)->data)
			return NULL;
		else if (key < (*p)->data)
			p = &(*p)->left;
		else
			p = &(*p)->right;
	}

	if ((new = (NODE*)malloc(sizeof(NODE))) == NULL)
		printf("error\n");

	new->left = new->right = NULL;
	new->data = key;
	*p = new;
}

void inorder(NODE* p)
{
	if (p == NULL)
		return;
	else
	{
		inorder(p->left);
		printf(" %d", p->data);
		inorder(p->right);
	}
}

void preorder(NODE* p)
{
	if (p == NULL)
		return;
	else
	{
		printf(" %d", p->data);
		preorder(p->left);
		preorder(p->right);
	}
}

void print_tree(NODE* p)
{
	inorder(p);
	printf("\n");
	preorder(p);
	printf("\n");
}

void find(int key)
{
	NODE* p = root;

	while (p != NULL)
	{
		if (key == p->data)
		{
			printf("yes\n");
			return;
		}
		else if (key < p->data)
			p = p->left;
		else
			p = p->right;
	}

	printf("no\n");
}

int main()
{
	int m;
	char str[10];
	int num;

	scanf("%d", &m);

	int i;

	for (i = 0; i < m; i++)
	{
		scanf("%s",str);
		if (strcmp(str, "insert") == 0)
		{
			scanf("%d", &num);
			insert(num);
		}
		else if (strcmp(str, "find") == 0)
		{
			scanf("%d", &num);
			find(num);
		}
		else
			print_tree(root);
	}

	return 0;
}