Description:
in-order=[13,14,15,20,23,22,28]
level=[20,14,23,13,15,22,28]
In level order array first element is root of the tree.20 is the root element.
now search this root element in the given in-order array when root element is find in this in-order

array.now create newroot take this as a root and return new root recursively. store inorder index

point as a new index position to paas in left subtree and right subtree for calling recursive Bstree().

20
left subtree / \ right subtree
[13,14,15] [23,22,28]
20
left subtree / \ right subtree
14 22
/ \ / \
13 15 23 28

Code:

class Node:
def __init__(self, data):
self.left = None
self.right = None
self.node = data
def Bstree(level,inorder):
# check if inorder have element or not.
if inorder:
for i in range(0,len(level)):
# check if level element is equal to in-order element.if both element is
# equal then create new node and store the same value in it.
if level[i] in inorder:
new_root=Node(level[i])
#store index value of level elementin new variable which is same in inorder element. this new index value is passed inside recursive function.
io_new=inorder.index(level[i])
break
if not inorder:
return new_root
#index passes in left subtree in range start to io_new-1
new_root.left=Bstree(level,inorder[0:io_new])
#index passes in right subtree in range io_new to length of inorder.
new_root.right=Bstree(level,inorder[io_new+1:len(inorder)])
return new_root
def traverse_inorder(root):
if root==None:
return
traverse_inorder(root.left)
print(root.node,end=' ')
traverse_inorder(root.right)
level=[20,14,23,13,15,22,28]
inorder=[13,14,15,20,23,22,28]
a=len(inorder)
root=Bstree(level,inorder)
traverse_inorder(root)
Output:
13 14 15 20 23 22 28

## Comments