Answered step by step
Verified Expert Solution
Question
1 Approved Answer
class ExpTree: def _ _ init _ _ ( self , rootObj ) : self.key = rootObj self.leftChild = None self.rightChild = None def insertLeft
class ExpTree:
def initself rootObj:
self.key rootObj
self.leftChild None
self.rightChild None
def insertLeftself newNode:
if self.leftChild is None:
self.leftChild ExpTreenewNode
else:
t ExpTreenewNode
tleftChild self.leftChild
self.leftChild t
def insertRightself newNode:
if self.rightChild is None:
self.rightChild ExpTreenewNode
else:
t ExpTreenewNode
trightChild self.rightChild
self.rightChild t
def getRightChildself:
return self.rightChild
def getLeftChildself:
return self.leftChild
def setRootValself obj:
self.key obj
def getRootValself:
return self.key
@staticmethod
def maketreepostfixexpression:
if isinstancepostfixexpression, str:
tokens postfixexpression.split
elif isinstancepostfixexpression, list:
tokens postfixexpression
else:
raise ValueErrorInvalid postfixexpression type"
stack Stack
for token in tokens:
if in token:
stack.pushExpTreefloattoken
elif token.isnumeric:
stack.pushExpTreeinttoken
elif token in :
if token :
if stack.size:
raise ValueErrorInvalid postfixexpression"
righttree stack.pop
lefttree stack.pop
newtree ExpTreetoken
newtree.insertLeftlefttree
newtree.insertRightrighttree
stack.pushnewtree
else:
if stack.size:
raise ValueErrorInvalid postfixexpression"
righttree stack.pop
lefttree stack.pop
newtree ExpTreetoken
newtree.insertLeftlefttree
newtree.insertRightrighttree
stack.pushnewtree
if stack.size:
raise ValueErrorInvalid postfixexpression"
return stack.pop
@staticmethod
def inordertree:
if tree:
if tree.leftChild and tree.rightChild:
return fExpTreeinordertreeleftChildtreekeyExpTreeinordertreerightChild
else:
return tree.key
@staticmethod
def postordertree:
if tree:
if tree.leftChild and tree.rightChild:
return fExpTreepostordertreeleftChildExpTreepostordertreerightChildtreekey
else:
return ftreekey
@staticmethod
def preordertree:
if tree:
if tree.leftChild and tree.rightChild:
return ftreekeyExpTreepreordertreeleftChildExpTreepreordertreerightChild
else:
return ftreekey
def evaluateself:
if isinstanceselfkey, ExpTree:
return self.key.evaluate
elif isinstanceselfkey, int or isinstanceselfkey, float:
return floatselfkey
else:
leftval self.leftChild.evaluate if self.leftChild else
rightval self.rightChild.evaluate if self.rightChild else
if self.key :
return leftval rightval
elif self.key :
return leftval rightval
elif self.key :
return leftval rightval
elif self.key :
return leftval rightval
elif self.key :
return leftval rightval
def strself:
if self.leftChild and self.rightChild:
return fstrselfleftChildselfkeystrselfrightChild
else:
return self.key
I dont understand how my code does not pass the exptree test conditions
# test an ExpTree
try:
postfix split
tree ExpTree.maketreepostfix
printstrtree
assert strtree
score
except Exception:
pass
try:
assert ExpTree.inordertree
assert ExpTree.postordertree
assert ExpTree.preordertree
score
except Exception:
pass
try:
assert ExpTree.evaluatetree
score
except Exception:
pass
try:
postfix split
tree ExpTree.maketreepostfix
assert strtree
assert ExpTree.inordertree
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started