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 (Python):



class Node:
    def __init__(self,key):
        self.key=key
        self.parent=self.left=self.right=None
    

class BST:
    def __init__(self):
        self.root=None
    def insert(self,key):
        node=Node(key)
        insert(self,node)


def insert(t,z):
    x=t.root
    y=None
    while x:
        y=x
        if z.key<x.key:
            x=x.left
        else:
            x=x.right
    z.parent=y
    
    if y==None:
        t.root=z
    elif z.key<y.key:
        y.left=z
    else:
        y.right=z

def preorder(x):
    print(" "+str(x.key),end="")
    if x.left != None:
        preorder(x.left)
    if x.right != None:
        preorder(x.right)
        
def inorder(x):
    if x.left != None:
        inorder(x.left)
    print(" "+str(x.key),end="")
    if x.right != None:
        inorder(x.right)

def find(t,k):
    while t:
        if t.key==k:
            return True
        if t.key > k:
            t=t.left
        else:
            t=t.right
    return False



n=int(input())
t=BST()
for i in range(n):
    tmp=input().split()
    if tmp[0]=="insert":
        t.insert(int(tmp[1]))
        
    elif tmp[0]=="find":
        if find(t.root,int(tmp[1])):
            print("yes")
        else:
            print("no")
        
    else:
        inorder(t.root)
        print("")
        preorder(t.root)
        print("")

Solution (Python):
class BinaryTree:
    class Node:
        def __init__(self, n):
            self.num = n
            self.parent = -1
            self.right = None
            self.left = None

    
    def __init__(self):
        self.root = None


    def insert(self, n):
        if not self.root:
            node = self.Node(n)
            self.root = node
        else:
            current, parent = self.root, None
            while current:
                parent = current
                if n < current.num:
                    current = current.left
                else:
                    current = current.right
            node = self.Node(n)
            node.parent = parent
            if n < parent.num:
                parent.left = node
            else:
                parent.right = node
                
            
    def find(self, n):
        if not self.root:
            return None
        current = self.root
        while current:
            if current.num == n:
                print('yes')
                return 
            elif n < current.num:
                current = current.left
            else:
                current = current.right
        print('no')












    
    def print_preorder(self, node):
        if not node.left and not node.right:
            return [node.num]
        elif not node.left:
            return [node.num] + self.print_preorder(node.right)
        elif not node.right:
            return [node.num] + self.print_preorder(node.left)
        else:
            return [node.num] + self.print_preorder(node.left) + self.print_preorder(node.right)


    def print_inorder(self, node):
        if not node.left and not node.right:
            return [node.num]
        elif not node.left:
            return [node.num] + self.print_inorder(node.right)
        elif not node.right:
            return self.print_inorder(node.left) + [node.num]
        else:
            return self.print_inorder(node.left) + [node.num] + self.print_inorder(node.right)


    def print_postorder(self, node):
        if not node.left and not node.right:
            return [node.num]
        elif not node.left:
            return self.print_inorder(node.right) + [node.num]
        elif not node.right:
            return self.print_inorder(node.left) + [node.num]
        else:
            return self.print_inorder(node.left) + self.print_inorder(node.right) + [node.num]



if __name__ == '__main__':
    n = int(input())
    qs = [ input().rstrip().split() for _ in range(n) ]
    bt = BinaryTree()
    for q in qs:
        if q[0] == 'insert':
            bt.insert(int(q[1]))
        elif q[0] == 'find':
            bt.find(int(q[1]))
        elif q[0] == 'print':
            print(' ' + ' '.join(map(str,bt.print_inorder(bt.root))))
            print(' ' + ' '.join(map(str,bt.print_preorder(bt.root))))

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array





sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


class BinaryNode:
    def __init__(self, key, left=None, right=None):
        self.val = key
        self.left = left
        self.right = right


def insert(root, node):
    if root is None:
        root = node
    else:
        if root.val < node.val:
            if root.right is None:
                root.right = node
            else:
                insert(root.right, node)
        else:
            if root.left is None:
                root.left = node
            else:
                insert(root.left, node)


def preorder(root):
    ret = []
    if root is None: return ret
    ret.append(root.val)
    ret.extend(preorder(root.left))
    ret.extend(preorder(root.right))
    return ret


def inorder(root):
    ret = []
    if root is None: return ret
    ret.extend(inorder(root.left))
    ret.append(root.val)
    ret.extend(inorder(root.right))
    return ret


def find(node, key):
    while node is not None and key != node.val:
        if key < node.val:
            node = node.left
        else:
            node = node.right
    return node


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    s, key = sl()
    root = BinaryNode(int(key))

    for n in range(N - 1):
        input = sl()
        if input[0] == 'print':
            print("", *inorder(root))
            print("", *preorder(root))
        elif input[0] == 'find':
            node = find(root, int(input[1]))
            print('yes' if node is not None else 'no')
        else:
            insert(root, BinaryNode(int(input[1])))


if __name__ == '__main__':
    main()

Solution (Python):




class Node:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None
        self.parent = None

class BinarySearchTree:
    def __init__(self):
        self.root = None
        self.count = 0
    
    def insert(self, node: Node):
        self.count += 1
        if not self.root:
            self.root = node
            return

        cur = self.root
        while cur:
            if node.val > cur.val:
                if cur.right:
                    cur = cur.right
                    continue
                else:
                    cur.right,node.parent = node,cur
                    return
            else:
                if cur.left:
                    cur = cur.left
                    continue
                else:
                    cur.left,node.parent = node,cur
                    return

    def find(self, val):
        cur = self.root
        while cur:
            if cur.val == val:
                return "yes"
            elif cur.val > val:
                cur = cur.left
            else:
                cur = cur.right
        return "no"


def main():

    
    preorder = []

    def preorder_walk(node):
        preorder.append(str(node.val))
        if node.left:
            preorder_walk(node.left)
        if node.right:
            preorder_walk(node.right)

    

    inorder = []

    def inorder_walk(node):
        if node.left:
            inorder_walk(node.left)
        inorder.append(str(node.val))
        if node.right:
            inorder_walk(node.right)


    n = int(input())
    
    bst = BinarySearchTree()
    
    for _ in range(n):
        op = input().strip().split()
        if op[0] == "insert":
            bst.insert(Node(int(op[1])))
        elif op[0] == "print":
            preorder,inorder = [],[]
            preorder_walk(bst.root)
            inorder_walk(bst.root)
            print(""," ".join(inorder))
            print("", " ".join(preorder))
        elif op[0] == "find":
            print(bst.find(int(op[1])))

if __name__ == "__main__":
    main()

Solution (Python):
class BinaryTree:
    def __init__(self, left, right, parent):
        self.left = left
        self.right = right
        self.parent = parent
        self.key = None
        self.root = False
        

def insert(T, z):
    global root
    
    y = -1
    try:
        x = root
    except:
        x = -1
        root = z
        T[z].root = True
    
    while x != -1:
        y = x
        if T[z].key < T[x].key:
            x = T[x].left
        else:
            x = T[x].right
    T[z].parent = y
    
    if y == -1:
        pass
    
    elif T[z].key < T[y].key:
        T[y].left = z
        
    else:
        T[y].right = z

def find(T, z, s):
    if s == -1:
        return False
        
    elif T[s].key == z:
        return True
        
    elif T[s].key < z:
        return find(T, z, T[s].right)
        
    else:
        return find(T, z, T[s].left)
    
def preParse(u):
    if u == -1:
        return 0
    print(" " + str(T[u].key), end="")
    preParse(T[u].left)
    preParse(T[u].right)

def inParse(u):
    if u == -1:
        return 0
    inParse(T[u].left)
    print(" " + str(T[u].key), end="")
    inParse(T[u].right)

m = int(input().rstrip())

z = 0
T = []
for _ in range(m):
    T.append(BinaryTree(-1, -1, -1))

for _ in range(m):
    command = input().rstrip()
    if command =="print":
        inParse(root)
        print()
        preParse(root)
        print()
    else:
        command, key = command.split(" ")
        key = int(key)
    
    
    if command == "insert":
        T[z].key = key
        insert(T, z)
        z += 1
        
    elif command == "find":
        if find(T, key, root):
            print("yes")
        else:
            print("no")

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

class Node:
    def __init__(self,parent,left,right):
        self.parent = parent
        self.left = left
        self.right = right

def insert(T:dict,z:int):
    global root
    y = -1
    x = root
    while x != -1:
        y = x
        if z < x:
            x = T[x].left
        else:
            x = T[x].right
    T[z] = Node(y,-1,-1)
    
    if y == -1:
        root= z
    elif z < y:
        T[y].left = z
    else:
        T[y].right = z

def find(T:dict,x:int,k:int):
    while x != -1 and k != x:
        if k < x:
            x = T[x].left
        else:
            x = T[x].right
    return x

def inorder(u:int):
    if u == -1:
        return 
    inorder(T[u].left)
    print(" "+str(u),end="")
    inorder(T[u].right)

def preorder(u:int):
    if u == -1:
        return 
    print(" "+str(u),end="")
    preorder(T[u].left)
    preorder(T[u].right)

n = int(input())
root = -1
T = {}
for _ in range(n):
    scan = input().split()
    if len(scan) == 1:
        inorder(root)
        print()
        preorder(root)
        print()
    else:
        k = int(scan[-1])
        if scan[0] == "insert":
            insert(T,k)
        else:
            print("yes" if find(T,root,k) != -1 else "no")

Solution (Python):
class Node:
    def __init__(self, key, parent=None, left=None, right=None):
        self.key = key
        self.parent = parent
        self.left = left
        self.right = right

class Tree:
    root = None
    preorder_list = []
    inorder_list = []
    def __init__(self):
        pass
    def insert(self, node):
        y = None
        x = self.root

        while x is not None:
            y = x
            if node.key < x.key:
                x = x.left
            else:
                x = x.right
        node.parent = y

        if y == None:
            self.root = node
        elif node.key < y.key:
            y.left = node
        else:
            y.right = node
    def find(self, node):
        y = None
        x = self.root
        while x is not None:
            if node.key == x.key:
                print('yes')
                return
            elif node.key < x.key:
                x = x.left
            else:
                x = x.right
        print('no')

    def display(self):
        
        self.inorder(self.root)
        self.preorder(self.root)
        print(' '+' '.join(map(str, self.inorder_list)))
        print(' '+' '.join(map(str, self.preorder_list)))
        pass

    def preorder(self, node):
        if node == None:
            return
        self.preorder_list.append(node.key)
        if node.left is not None:
            self.preorder(node.left)
        if node.right is not None:
            self.preorder(node.right)

    def inorder(self, node):
        if node == None:
            return
        if node.left is not None:
            self.inorder(node.left)
        self.inorder_list.append(node.key)
        if node.right is not None:
            self.inorder(node.right)

n = int(input())
command_list = [list(input().split()) for _ in range(n)]

tree = Tree()
for command in command_list:
    if command[0] == 'insert':
        node = Node(int(command[1]))
        tree.insert(node)
        
    if command[0] == 'print':
        tree.display()
        tree.preorder_list = []
        tree.inorder_list = []
    if command[0] == 'find':
        tree.find(Node(int(command[1])))

Solution (Python):
class Node:
    def __init__(self, key):
        self.key = key
        self.parent = None
        self.left = self.right = None
    def preorder(self):
        L = [self.key]
        if self.left: L += self.left.preorder()
        if self.right: L += self.right.preorder()
        return L
    def inorder(self):
        L = []
        if self.left: L += self.left.inorder()
        L.append(self.key)
        if self.right: L += self.right.inorder()
        return L

def insert(t, z):
    y = None
    x = t
    while x:
        y = x 
        x = x.left if z.key < x.key else x.right
    z.parent = y
    if not y: 
        return z
    if z.key < y.key:
        y.left = z 
    else:
        y.right = z 
    return t

def find(t, k):
    x = t
    while x:
        if x.key == k:
            return True
        x = x.left if k < x.key else x.right
    return False

t = None
m = int(input())
for _ in range(m):
    cmd = list(input().split())
    if cmd[0] == 'insert':
        t = insert(t, Node(int(cmd[1])))
    if cmd[0] == 'find':
        print('yes' if find(t, int(cmd[1])) else 'no')
    if cmd[0] == 'print':
        print('', *t.inorder())
        print('', *t.preorder())

Solution (Python):
n=int(input())
tree={}
root=None

def insert(tree,number):
    parent=None
    children=root
    while children!=None:
        parent=children
        if children>number:
            children=tree[parent][0]
        else:
            children=tree[parent][1]
    
    if parent>number:
        tree[parent][0]=number
    else:
        tree[parent][1]=number

def preorder(tree,i):
    if i==None:
        return
    print(" {}".format(str(i)),end="")
    preorder(tree,tree[i][0])
    preorder(tree,tree[i][1])

def inorder(tree,i):
    if i==None:
        return
    inorder(tree,tree[i][0])
    print(" {}".format(str(i)),end="")
    inorder(tree,tree[i][1])

def find(x,key):
    while x is not None and key!=x:
        if key<x:x=tree[x][0]
        else:x=tree[x][1]
    return x

for i in range(n):
    tmp=list(input().split())
    if tmp[0]=="print":
        inorder(tree,root)
        print()
        preorder(tree,root)
        print()
    else:
        number=int(tmp[1])
        if tmp[0]=="insert":
            if root is None:
                root=number
                tree[number]=[None,None]
            else:
                tree[number]=[None,None]
                insert(tree,number)
        else:
            if find(root,int(tmp[1])) is not None:print("yes")
            else:print("no")

Solution (Python):
import sys






class Node():
    __slots__ = ['key', 'left', 'right']
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None


def insert(key):
    global root
    x = root 
    y = None  
    while x:
        y = x 
        if key < x.key:
            x = x.left 
        else:
            x = x.right 
    
    if root is None:
        root = Node(key)
    elif key < y.key: 
        y.left = Node(key)
    else: 
        y.right = Node(key) 
        
        
def find(key):
    x = root
    while x:
        if key==x.key:
            print('yes')
            return
        elif key < x.key:
            x = x.left
        else:
            x = x.right
    print('no')
    

def inorder(inorder_list, node):
    if node.left is not None:
        inorder(inorder_list, node.left)
    inorder_list.append(str(node.key))
    if node.right is not None:
        inorder(inorder_list, node.right)
    return inorder_list
        
def preorder(preorder_list, node):
    preorder_list.append(str(node.key))
    if node.left is not None:
        preorder(preorder_list, node.left)
    if node.right is not None:
        preorder(preorder_list, node.right)
    return preorder_list
        
root = None

input()
for o in sys.stdin:
    if o[0]=='i':
        insert(int(o[7:]))
    elif o[0]=='f':
        find(int(o[5:]))
    else:
        x = root
        inorder_list = inorder([], x)
        print(' '+' '.join(inorder_list))
        preorder_list = preorder([], x)
        print(' '+' '.join(preorder_list))

Solution (Python):
import sys
sys.setrecursionlimit(2**20)  


class Node:
    def __init__(self, parent=None, left=None, right=None):
        self.parent = parent
        self.left = left  
        self.right = right


def pre_parse(T, u, pre_ls):
    if u == None:
        return
    pre_ls.append(u)
    pre_parse(T, T[u].left, pre_ls) 
    pre_parse(T, T[u].right, pre_ls)


def in_parse(T, u, in_ls):
    if u == None:
        return
    in_parse(T, T[u].left, in_ls)
    in_ls.append(u)
    in_parse(T, T[u].right, in_ls)


def insert(T, ROOT, z):
    if ROOT == z:
        T[z] = Node()
    else:
        T[z] = Node()  
        x = ROOT
        while x is not None:  
            next_parent = x  
            if z < x:  
                x = T[x].left
            else:
                x = T[x].right
        T[z].parent = next_parent
        if z < next_parent:
            T[next_parent].left = z
        else:
            T[next_parent].right = z


def find(T, ROOT, k):
    x = ROOT
    while x and k != x:
        if k < x:
            x = T[x].left
        else:
            x = T[x].right
    return x

def print_result(T):
    pre_ls, in_ls = [], []
    in_parse(T, ROOT, in_ls)
    pre_parse(T, ROOT, pre_ls)
    print('', *in_ls)
    print('', *pre_ls)


n = int(input())
T = {}
for i in range(n):
    tmp = input()
    if i == 0:
        ROOT = int(tmp[7:])
    if tmp[0] == "p":
        print_result(T)
    elif tmp[0] == "f":
        if find(T, ROOT, int(tmp[5:])):
            print("yes")
        else:
            print("no")
    else:
        insert(T, ROOT, int(tmp[7:]))

Solution (Python):
import sys, collections
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

class Node():
    key = None
    p = None
    left = None
    right = None

    def __init__(self, key):
        self.key = key

class BinarySearchTree():
    root = None

    def insert(self, z):
        y = None
        x = self.root
        while x is not None:
            y = x
            if z.key < x.key:
                x = x.left
            else:
                x = x.right
        
        z.p = y

        if y is None:
            self.root = z
        elif z.key < y.key:
            y.left = z
        else:
            y.right = z

    def find(self, k):
        x = self.root
        while x is not None:
            if x.key==k:
                return True
            elif x.key>k:
                x = x.left
            else:
                x = x.right
        return False

    def print_nodes(self):
        inorder = []
        preorder = []

        def dfs(v):
            v_key = v.key
            preorder.append(v_key)
            if v.left is not None:
                dfs(v.left)
            inorder.append(v_key)
            if v.right is not None:
                dfs(v.right)

        dfs(self.root)

        print('', *inorder)
        print('', *preorder)

def resolve():
    m = I()
    T = BinarySearchTree()
    for _ in range(m):
        q = LSS()
        if q[0]=='insert':
            k = int(q[1])
            z = Node(k)
            T.insert(z)
        elif q[0]=='find':
            k = int(q[1])
            if T.find(k):
                print('yes')
            else:
                print('no')
        else:
            T.print_nodes()

if __name__ == '__main__':
    resolve()

Solution (Python):
import sys

sys.setrecursionlimit(2 ** 20)  


class Node:
    def __init__(self,
                 left,
                 right):
        self.left = left
        self.right = right


def insert(t: dict, i: int, r: int):
    if r > i:
        if t[r].left is not None:
            insert(t, i, t[r].left)
        else:
            t[r].left = i
    if r < i:
        if t[r].right is not None:
            insert(t, i, t[r].right)
        else:
            t[r].right = i


def inorder(t: dict, p: int, in_ls: list):
    if t[p].left is not None:
        inorder(t, t[p].left, in_ls)
    in_ls.append(p)
    if t[p].right is not None:
        inorder(t, t[p].right, in_ls)


def preorder(t: dict, p: int, pre_ls: list):
    pre_ls.append(p)
    if t[p].left is not None:
        preorder(t, t[p].left, pre_ls)
    if t[p].right is not None:
        preorder(t, t[p].right, pre_ls)


N = int(input())
T = {}
in_list = []
pre_list = []

order = list(map(str, input().split()))
root = int(order[1])
T[root] = Node(None, None)

for _ in range(N-1):
    order = str(input())
    if order[0] == 'i':
        order = list(order.split())
        T[int(order[1])] = Node(None, None)
        insert(T, int(order[1]), root)
    elif order[0] == 'f':
        order = list(order.split())
        if int(order[1]) in T:
            print('yes')
        else:
            print('no')
    else:
        inorder(T, root, in_list)
        print('', *in_list)
        preorder(T, root, pre_list)
        print('', *pre_list)
        in_list = []
        pre_list = []

Solution (Python):
class BinarySearchTree:
    def __init__(self):
        self.root = None
        self.nodes = []
        self.keys = set()

    def insert(self, z):
        self.keys.add(z)
        z = Node(z)
        self.nodes.append(z)
        y = None
        x = self.root
        while x is not None:
            y = x
            if z.key < x.key:
                x = x.left
            else:
                x = x.right
            z.parent = y

        if y is None:
            self.root = z
        elif z.key < y.key:
            y.left = z
        else:
            y.right = z
    
    def dfs(self, node):
        self.preorder.append(node.key)
        if node.left is not None:
            self.dfs(node.left)

        self.inorder.append(node.key)

        if node.right is not None:
            self.dfs(node.right)

    def out(self):
        self.preorder = []
        self.inorder = []
        self.dfs(self.root)

        print("", *self.inorder)
        print("", *self.preorder)

    def find(self, key):
        if key in self.keys:
            print("yes")
        else:
            print("no")


class Node:
    def __init__(self, v):
        self.key = v
        self.left = None
        self.right = None
        self.parent = None


def main():
    T = BinarySearchTree()
    n = int(input())
    for i in range(n):
        command = input().split()
        if command[0] == "print":
            T.out()
        elif command[0] == "find":
            T.find(int(command[1]))
        else:
            T.insert(int(command[1]))


if __name__ == "__main__":
    main()

Solution (Python):
NIL = -1
class Node():
    def __init__(self, val):
        self.val = val
        self.parent = NIL
        self.left = NIL
        self.right = NIL
    def __str__(self):
        return "val:{}, parent:{}, left:{}, right:{}".format(self.val, self.parent, self.left, self.right)
        
class Tree():
    def __init__(self):
        self.Nodes = {}
        self.Nodes[NIL] = Node(NIL)
        self.root = self.Nodes[NIL]
        
    def insert(self, node):
        y = self.Nodes[NIL]
        x = self.root
        while x.val != NIL:
            y = x
            if(node.val < x.val):
                x = self.Nodes[x.left]
            else:
                x = self.Nodes[x.right]
        node.parent = y.val
        
        if(y.val == NIL):
            self.root = node
        elif(node.val < y.val):
            y.left = node.val
        else:
            y.right = node.val
        self.Nodes[node.val] = node
    
    def find(self, x):
        cur_n = self.root
        while (cur_n.val != NIL and cur_n.val != x):

            if(x < cur_n.val):
                cur_n = self.Nodes[cur_n.left]
            else:
                cur_n = self.Nodes[cur_n.right]
        return "yes" if cur_n.val != NIL else "no"

    def getPreOrder(self):
        return self.preParse(self.root.val, [])    
        
    def getInOrder(self):
        return self.inParse(self.root.val, [])
    
    def getPostOrder(self):
        return self.postParse(self.root.val, [])
        
    def inParse(self, u, res):
        if(u == NIL):
            return
        self.inParse(self.Nodes[u].left, res)
        res += [u]
        self.inParse(self.Nodes[u].right, res)
        return res

    def postParse(self, u, res):
        if(u == NIL):
            return
        self.postParse(self.Nodes[u].left, res)
        self.postParse(self.Nodes[u].right, res)
        res += [u]
        return res
    
    def preParse(self, u, res):
        if(u == NIL):
            return 
        res += [u]
        self.preParse(self.Nodes[u].left, res)
        self.preParse(self.Nodes[u].right, res)
        return res
        
n = int(input())

tree = Tree()
for row in range(n):
    operate = input().split()
    if(operate[0] == "insert"):
        tree.insert(Node(int(operate[1])))
        
    if(operate[0] == "print"):
        print(" {}".format( " ".join([str(ss) for ss in tree.getInOrder()])))
        print(" {}".format( " ".join([str(ss) for ss in tree.getPreOrder()])))
        
    if(operate[0] == "find"):
        print(tree.find(int(operate[1])))

Solution (Python):
from collections import deque
import sys
readline = sys.stdin.readline
write = sys.stdout.write

class Node:
    __slots__ = ['key', 'left', 'right']
    def __init__(self, key):
        self.key = key
        self.left = self.right = None


root = None

def insert(key):
    global root
    x = root 
    y = None 
    while x is not None:
        y = x
        if key < x.key:
            x = x.left 
        else:
            x = x.right  

    if y is None: 
        root = Node(key)
    elif key < y.key:
        y.left = Node(key) 
    else:
        y.right = Node(key) 







def find(target):
    result = root
    while result and target != result.key:
        result = result.left if target < result.key else result.right
    return result is None 

def inorder(node):
    return   inorder(node.left) + " "+ str(node.key) + inorder(node.right) if node else ''
def preorder(node):
    return   " " + str(node.key) + preorder(node.left) + preorder(node.right) if node else ''
input()
for e in sys.stdin:
    if e[0] == "i":
        insert(int(e[7:]))
    elif e[0] == 'f':
        if find(int(e[5:])):
            print("no")
        else:
            print("yes")
        
    else:
        print(inorder(root))
        print(preorder(root))