You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree.
You just have to complete the function.

Input Format

You are given a function,

Node * insert (Node * root ,int data) {

}

Constraints

No. of nodes in the  tree $\leq$ 500

Output Format

Return the root of the binary search tree after inserting the value into the tree.

Sample Input
        4
       / \
      2   7
     / \
    1   3

The value to be inserted is 6.

Sample Output
         4
       /   \
      2     7
     / \   /
    1   3 6

Source: hackerrank | Difficulty: EASY
Tags: Tree algorithms


Solutions:
class Node:

	def __init__(self, info):
		self.info = info
		self.left = None
		self.right = None
		self.level = None

	def __str__(self):
		return str(self.info)

def preOrder(a):
	if a == None:
		return
	print(a.info, end=' ')
	try:
		preOrder(a.left.root)
	except AttributeError:
		print('', end='')
	try:
		preOrder(a.right.root)
	except AttributeError:
		print('', end='')

class BinarySearchTree:

	def __init__(self):
		self.root = None

	def insert(self, val):
		if self.root is None:
			self.root = Node(val)
		elif val > self.root.info:
			if self.root.right is None:
				self.root.right = BinarySearchTree()
				self.root.right.root = Node(val)
			else:
				self.root.right.insert(val)
		elif val < self.root.info:
			if self.root.left is None:
				self.root.left = BinarySearchTree()
				self.root.left.root = Node(val)
			else:
				self.root.left.insert(val)
		return self.root
tree = BinarySearchTree()
t = int(input())
arr = list(map(int, input().split()))
for i in range(t):
	tree.insert(arr[i])
preOrder(tree.root)