Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python problem! builds the expression tree from expr. Do not change other functions. Test code is given at the end. Thank you very much! class

Python problem! builds the expression tree from expr. Do not change other functions. Test code is given at the end. Thank you very much!

class expressionTree: # constructTree(expr) builds the expression tree from expr # Here you can assume: # expr is an arithmetic expression with an answer value # The first non-space symbol is not "-" # (because we can add "0" to the left if so) # # getExpr() to reconstruct expr from the tree # # revPolish() to get the reverse polish notatation # # and maybe more...

class treeNode: def __init__(self, value, lchild, rchild): self.value, self.lchild, self.rchild = value, lchild, rchild

def __init__(self): self.treeRoot = None

#utility functions for constructTree def mask(self, s): # The function empties the inside of every outermost parentheses pair # and return it as a string # # e.g. s = ( 2 + 3^2 -(2-4)) + 2 - (-1 + 1/7) # It returns ( ) + 2 - ( ) # # --- code ----#

#Build expression tree from expr, set it to self.treeRoot def constructTree(self, expr): temp = 1 #input = the expression tree whose root is self.treeRoot #output = reconstructed arithmetic expression as a def getExpr(self): temp = 1 #input = the expression tree whose root is self.treeRoot #output = string that is the reverse polish notation def revPolish(self): temp = 1

# To check the mask function e = expressionTree() s =" ( 2 + 3*(2- 1/3))+ 2 - ((5-2/3)*3 - 2)" print(e.mask(s)) # should print # ( )+ 2 - ( )

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions

Question

What is the use of a tagline?

Answered: 1 week ago

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago