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

use std::io::{self, Read};
use std::str::FromStr;

pub struct Scanner<R: Read> {
    reader: R,
}
impl<R: Read> Scanner<R> {
    pub fn new(reader: R) -> Scanner<R> {
        Scanner { reader: reader }
    }
    pub fn read<T: FromStr>(&mut self) -> T {
        let s = self
            .reader
            .by_ref()
            .bytes()
            .map(|c| c.expect("failed to read char") as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        s.parse::<T>().ok().expect("failed to parse token")
    }
}

enum Tree {
    Nil,
    Node {
        key: i64,
        left: Box<Tree>,
        right: Box<Tree>,
    },
}

#[allow(dead_code)]
impl Tree {
    fn insert(&mut self, val: i64) {
        match *self {
            Tree::Nil => {
                *self = Tree::Node{ 
                    key: val, 
                    left: Box::new(Tree::Nil), 
                    right: Box::new(Tree::Nil),
                }
            },
            Tree::Node{ref key, ref mut left, ref mut right} => {
                if *key < val {
                    right.insert(val);
                } else {
                    left.insert(val);
                }
            },
        }
    }

    fn find(&self, val: i64) -> bool {
        if let Tree::Node{ref key, ref left, ref right} = *self {
            if *key == val {
                return true;
            } else if *key < val {
                return right.find(val);
            } else {
                return left.find(val);
            }
        } else {
            return false;
        }
    }

    fn print_pre(&self) {
        if let Tree::Node{ref key, ref left, ref right} = *self {
            print!(" {}", key);
            left.print_pre();
            right.print_pre();
        }
    }
    
    fn print_in(&self) {
        if let Tree::Node{ref key, ref left, ref right} = *self {
            left.print_in();
            print!(" {}", key);
            right.print_in();
        }
    }

    fn print_post(&self) {
        if let Tree::Node{ref key, ref left, ref right} = *self {
            left.print_post();
            right.print_post();
            print!(" {}", key);
        }
    }
}


fn main() {
    let sin = io::stdin();
    let sin = sin.lock();
    let mut sc = Scanner::new(sin);

    let n: usize = sc.read();
    let mut tree = Tree::Nil;

    for _ in 0..n {
        let cmd: String = sc.read();
        match cmd.as_str() {
            "insert" => {
                let val: i64 = sc.read();
                tree.insert(val);
            },
            "find" => {
                let val: i64 = sc.read();
                if tree.find(val) {
                    println!("yes");
                } else {
                    println!("no");
                }
            },
            "print" => {
                tree.print_in();
                println!("");
                tree.print_pre();
                println!("");
            },
            _ => { panic!("irregular command"); }
        }
    }
}