def isOperator(text): if text == '+' or text == '-' or text == '*' or text == '/': return True else: return False def createOperatorNode(text, i=0): """Returns (node, pointer to remainingTextAfterThisTree)""" #breakpoint() node = {} if not isOperator(text[i]): raise Exception('Expected operator, not ' + text.split()[i]) else: node['operator'] = text[i] i += 1 # Move the text index past the operator. while text[i] == ' ': i += 1 # Keep moving the text index until it points to a non-space character (either an operator or number). if isOperator(text[i]): # RECURSIVE CASE node['left'], i = createOperatorNode(text, i) # left child is an operator node else: node['left'] = int(text[i:].split()[0]) # left child is a number i += len(str(node['left'])) # Move the text index past the number. while text[i] == ' ': i += 1 # Keep moving the text index until it points to a non-space character (either an operator or number). if isOperator(text[i]): # RECURSIVE CASE node['right'], i = createOperatorNode(text, i) # right child is an operator node else: node['right'] = int(text[i:].split()[0]) # right child is a number i += len(str(node['right'])) # Move the text index past the number. while i