human
What is a code to construct Special Binary Tree from given Inorder traversal and the other steps required given the following:
Construct Special Binary Tree from given Inorder traversal | Recursive function to construct binary of size len from Inorder traversal inorder [ ] . Initial values of start and end should be 0 and len - 1..
Find index of the maximum element from Binary Tree.
Pick the maximum value and make it root.
If this is the only element in inorder [ start . . end ] , then return it.
Using index in Inorder traversal , construct left and right subtress.
Function to find index of the maximum value in arr [ start ... end ].
Helper class that allocates a new node with the given data and None left and right pointers ..
This funtcion is here just to test buildTree ( ).
first recur on left child.
then print the data of node.
now recur on right child.
Driver Code.
Assume that inorder traversal of following tree is given 40 / \ 10 30 / \ 5 28.
Let us test the built tree by printing Insorder traversal"
How can the above be solved in Python?
assistant
#Here is code to construct Special Binary Tree from given Inorder traversal and the other steps required.
 def buildTree ( inorder , start , end ) :
	if start > end :
		return None
	i = Max ( inorder , start , end )
	root = newNode ( inorder [ i ] )
	if start == end :
		return root
	root . left = buildTree ( inorder , start , i - 1 )
	root . right = buildTree ( inorder , i + 1 , end )
	return root
def Max ( arr , strt , end ) :
	i , Max = 0 , arr [ strt ]
	maxind = strt
	for i in range ( strt + 1 , end + 1 ) :
		if arr [ i ] > Max :
			Max = arr [ i ]
			maxind = i
	return maxind
class newNode :
	def __init__ ( self , data ) :
		self . data = data
		self . left = None
		self . right = None
def printInorder ( node ) :
	if node == None :
		return
	printInorder ( node . left )
	print ( node . data , end = " ▁ " )
	printInorder ( node . right )
if __name__ == ' _ _ main _ _ ' :
	inorder = [ 5 , 10 , 40 , 30 , 28 ]
	Len = len ( inorder )
	root = buildTree ( inorder , 0 , Len - 1 )
	print ( " Inorder ▁ traversal ▁ of ▁ the " , " constructed ▁ tree ▁ is ▁ " )
	printInorder ( root )