Answer to Question #261079 in Python for abc

Question #261079

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



1
Expert's answer
2021-11-04T16:26:22-0400
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


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog