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 PHP (judge verdict: Time Limit Exceeded)

<?php
solve1();

// 自力解
function solve1(){
	$n = (int)trim(fgets(STDIN));

	$list = [];
	for ($i=0; $i < $n; $i++) { 
		$input = explode(' ', trim(fgets(STDIN)));
		if ($input[0] === 'insert') {
			$node = new BTreeNode((int)$input[1]);
			$list[$node->getId()] = $node;
			insertNode($list, $node);
		}elseif ($input[0] === 'find') {
			if (findNode($list, (int)$input[1])) {
				echo 'yes' . PHP_EOL;
			}else{
				echo 'no' . PHP_EOL;
			}
		}else{
			$root = getRoot($list);
			if ($root != -1) {
				inorderWalk($list, $root);
				echo PHP_EOL;
				preorderWalk($list, $root);
				echo PHP_EOL;
			}else{
				echo PHP_EOL;
				echo PHP_EOL;
			}
		}
	}
}

function getRoot($list){
	$p = -1;
	if (count($list) > 0) {
		$p = $list[array_keys($list)[0]]->getId();
		while (true) {
			$tmp = $list[$p]->getParentId();
			if ($tmp === -1) {
				break;
			}
			$p = $tmp;
		}
	}
	return $p;
}
function insertNode($list, $node){
	$parent = -1;
	$cur = getRoot($list);
	while ($cur != -1) {
		$parent = $cur;
		if ($node->getId() < $list[$parent]->getId()) {
			$cur = $list[$parent]->getLeft();
		}else{
			$cur = $list[$parent]->getRight();
		}
	}
	if ($parent !== $node->getId()) {
		$node->setParentId($parent);
		if ($parent !== -1) {
			if ($node->getId() < $list[$parent]->getId()) {
				$list[$parent]->setLeft($node->getId());
			}else{
				$list[$parent]->setRight($node->getId());
			}
		}
	}

}

function findNode($list, $target){
	$cur = getRoot($list);
	while ($cur != -1) {
		if ($list[$cur]->getId() === $target) {
			break;
		}
		$parent = $cur;
		if ($target < $list[$parent]->getId()) {
			$cur = $list[$parent]->getLeft();
		}else{
			$cur = $list[$parent]->getRight();
		}
	}
	return ($cur != -1);
}

function preorderWalk($list, $id){
	echo ' ' . $id;
	if ($list[$id]->hasLeft()) {
		preorderWalk($list, $list[$id]->getLeft());
	}
	if ($list[$id]->hasRight()) {
		preorderWalk($list, $list[$id]->getRight());
	}
}

function inorderWalk($list, $id){
	if ($list[$id]->hasLeft()) {
		inorderWalk($list, $list[$id]->getLeft());
	}
	echo ' ' . $id;
	if ($list[$id]->hasRight()) {
		inorderWalk($list, $list[$id]->getRight());
	}
}

class BTreeNode
{
	private $id;
	private $depth = 0;
	private $height = 0;
	private $parent_id = -1;
	private $left = -1;
	private $right = -1;

	function __construct($id)
	{
		$this->id = $id;
	}
	function setParentId($p_id){
		$this->parent_id = $p_id;
	}
	function setLeft($l_id){
		$this->left = $l_id;
	}
	function setRight($r_id){
		$this->right = $r_id;
	}
	function getId(){
		return $this->id;
	}
	function getParentId(){
		return $this->parent_id;
	}
	function getLeft(){
		return $this->left;
	}
	function getRight(){
		return $this->right;
	}
	function hasChild(){
		return ($this->left !== -1) || ($this->right !== -1);
	}
	function getChildren(){
		$children = [];
		if ($this->hasLeft()) {
			$children[] = $this->left;
		}
		if ($this->hasRight()) {
			$children[] = $this->right;
		}
		return $children;
	}
	function getAnotherChild($c_id){
		$c = -1;
		if ($this->getLeft() === $c_id) {
			$c = $this->getRight();
		}elseif ($this->getRight() === $c_id) {
			$c = $this->getLeft();
		}
		return $c;
	}
	function hasLeft(){
		return $this->left !== -1;
	}
	function hasRight(){
		return $this->right !== -1;
	}
	function setDepth($d){
		$this->depth = $d;
	}
	function addDepth(){
		$this->depth += 1;
	}
	function getDepth(){
		return $this->depth;
	}
	function setHeight($h){
		$this->height = $h;
	}
	function getHeight(){
		return $this->height;
	}
}