Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

On python please ### EX7_21 # Complete constructTree below. Follow the instructions in the comment # - no change of function / class names class

On python please

### EX7_21 # Complete constructTree below. Follow the instructions in the comment # - no change of function / class names

class expressionTree: 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 - ( ) # nestLevel = 0 masked = list(s) for i in range(len(s)): if s[i]==")": nestLevel -=1 elif s[i]=="(": nestLevel += 1 if nestLevel>0 and not (s[i]=="(" and nestLevel==1): masked[i]=" " return "".join(masked)

def isNumber(self, expr): # s must be a non-empty string # returns True if it's convertible to float, else False if len(s) == 0 or not isinstance(s, str): print("type mismatch error: isNumber") return "type mismatch error: isNumber" # --- function code starts ---# try: s.replace(" ", "") float(s) return True except: return False # --- function code ends ---#

def _construct(self,expr): # First strip expr # If isNumber(expr) is true, it's a leaf. # Otherwise # - do s = mask(expr) # - find an operator of the lowest precedence in s # - If there is no operator in s, # and s[0]="(", s[len(s)-1]=")" # remove the outermost parenthese and pass the rest # to _constrct recursively # - otherwise pos = position of the discovered operator # pass expr[:pos] and expr[pos+1:] # to _constrct recursively # - Create a new treeNode # - Connect the three to return the root expr=expr.strip() if expr=="": return None elif self.isNumber(expr): return self.treeNode(expr, None, None) else: #--- code the rest ----

#Build the expression tree at self.treeRoot def constructTree(self, expr): # code one line to set self.treeRoot using _construct

# To check your constructTree e = expressionTree() expr =" 5*(2 / (3-1)) - (-1)*( 4 - 5*( 6- 3^(2-5) ) ) + 5 " e.constructTree(expr)

# The pseudo code in the lecture notes modified def getExpr(root): TL, TR = root.lchild, root.rchild if TL==None and TR==None: r = str(root.value) if float(r)<0: return "(" + r + ")" else: return r else: if TL==None: L = "" elif (root.value=="*" or root.value=="/" or root.value=="^") and (TL.value=="+" or TL.value=="-"): L = "(" + getExpr(TL) + ")" else: L = getExpr(TL) if TR==None: R = "" elif (root.value=="*" or root.value=="/" or root.value=="^") and (TR.value=="+" or TR.value=="-"): R = "(" + getExpr(TR) + ")" else: R = getExpr(TR) return L + str(root.value) + R print(getExpr(e.treeRoot)) ## This should print #5*2/(3-1)-(-1)*(4-5*(6-3^(2-5)))+5

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_2

Step: 3

blur-text-image_3

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

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions

Question

Compute the derivative of f(x)cos(-4/5x)

Answered: 1 week ago

Question

Discuss the process involved in selection.

Answered: 1 week ago

Question

Differentiate tan(7x+9x-2.5)

Answered: 1 week ago

Question

Explain the sources of recruitment.

Answered: 1 week ago

Question

Differentiate sin(5x+2)

Answered: 1 week ago