implement a simple symbolic equation solver. The equation must be stored in a binary tree.Each operand or operator should be stored as a tuple of the form (TYPE, VALUE).Complete the bodies of the insert, and evaluate methods. Include your solution in the sections mentioned as "Include your code here". Please do NOT change any of the code outside of these two methods.
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
def get_output(self)
value = self.evaluate()
if value > 999:
print('OVERFLOW')
elif value < 0:
print('UNDERFLOW')
else:
print(value)
# Your task is to implement the following methods. #
def insert(self, data, bracketed):
#Include your code here
return self
def evaluate(self):
#Include your code here
pass
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
def get_output(self):
value = self.evaluate()
if value > 999:
print('OVERFLOW')
elif value < 0:
print('UNDERFLOW')
else:
print(value)
# Your task is to implement the following methods. #
def insert(self, item):
new_node = Node(item)
print("next value", item)
if self.root == None:
print("first node", new_node)
self.root = new_node
self.size += 1
else:
inserted = False
current = self.root
while not inserted:
print(current)
if item < current.data:
if current.left == None:
current.left = new_node
inserted = True
self.size += 1
else:
current = current.left
else:
if current.right == None:
current.right = new_node
inserted = True
self.size += 1
else:
current = current.right
Comments
Leave a comment