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

import java.util.Scanner;

class Main {
	static StringBuilder sb = new StringBuilder("");
	static Node root = null;	//根っこ
	
	//名ばかりメイン
	public static void main(String args[]) {
		run();
	}
	
	//実質メイン
	public static void run() {
		Scanner scanner = new Scanner(System.in);
		int  num = scanner.nextInt();//要素数
		String ope;
		
		//入力
		for (int i = 0; i < num; i++) {
			ope = scanner.next();
			//命令分岐
			switch( Operate.getEnumName(ope) ){
			case in:
				insert( new Node( scanner.nextInt() ) );
				break;
			case find:
				if(find( scanner.nextInt() ) ){
					sb.append("yes\n");
				}else{
					sb.append("no\n");
				}
				break;
			case print:
				print();
			}
		}
		
		output();//出力関数
		
		scanner.close();
	}
	
	//挿入処理
	public static void insert(Node node){
		Node parent = null;				// x の親
		Node child = root;
		while (child != null) {
			parent = child;				// 親を設定
			if (node.key < child.key) {
				child = child.left;		// 左の子へ移動
			} else {
				child = child.right;	// 右の子へ移動
			}
		}
		
		//親か子か
		if (parent == null) {
			root = node;
		} else if (node.key < parent.key) {
			parent.left = node;
		} else {
			parent.right = node;
		}
	}
	
	//先行巡回
	public static void preParse(Node node) {
		if (node == null) return;
		
		sb.append(" ").append(node.key);
		preParse(node.left);
		preParse(node.right);
	}
	//中間巡回
	public static void inParse(Node node) {
		if (node == null) return;
		
		inParse(node.left);
		sb.append(" ").append(node.key);
		inParse(node.right);
	}
	
	//見つける
	public static boolean find(int key) {
		Node node = root;
		//見つかるまで掘る
		while (node != null) {
			if (key == node.key) {
				return true;
			} else if (key < node.key) {
				node = node.left;
			} else {
				node = node.right;
			}
		}
		//なければfalse
		return false;
	}
	
	//出力準備
	public static void print() {
		inParse(root);
		sb.append("\n");
		preParse(root);
		sb.append("\n");
	}
	
	//出力
	public static void output(){
		System.out.print(sb);
	}
	
	//enum宣言
	public static enum Operate{
		in("insert"),
		find("find"),
		print("print");
		
		private final String name;
		
		Operate(String name){
			this.name = name;
		}
		
		String getName(){
			return this.name;
		}

		public static Operate getEnumName(String str){
			for(Operate v : values()){
				if(v.getName().equals(str)){
					return v;
				}
			}
			throw new IllegalArgumentException("undefined : " + str);
		}
	}

}
//クラス
class Node{
	public int key;
	public Node left;
	public Node right;
	
	public Node(int key) {
		this.key = key;
		left = right = null;
	}
	
}