Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class ExpTree: def _ _ init _ _ ( self , rootObj ) : self.key = rootObj self.leftChild = None self.rightChild = None def insertLeft

class ExpTree:
def __init__(self, rootObj):
self.key = rootObj
self.leftChild = None
self.rightChild = None
def insertLeft(self, newNode):
if self.leftChild is None:
self.leftChild = ExpTree(newNode)
else:
t = ExpTree(newNode)
t.leftChild = self.leftChild
self.leftChild = t
def insertRight(self, newNode):
if self.rightChild is None:
self.rightChild = ExpTree(newNode)
else:
t = ExpTree(newNode)
t.rightChild = self.rightChild
self.rightChild = t
def getRightChild(self):
return self.rightChild
def getLeftChild(self):
return self.leftChild
def setRootVal(self, obj):
self.key = obj
def getRootVal(self):
return self.key
@staticmethod
def make_tree(postfix_expression):
if isinstance(postfix_expression, str):
tokens = postfix_expression.split()
elif isinstance(postfix_expression, list):
tokens = postfix_expression
else:
raise ValueError("Invalid postfix_expression type")
stack = Stack()
for token in tokens:
if '.' in token:
stack.push(ExpTree(float(token)))
elif token.isnumeric():
stack.push(ExpTree(int(token)))
elif token in '+-*/^':
if token =='^':
if stack.size()2:
raise ValueError("Invalid postfix_expression")
right_tree = stack.pop()
left_tree = stack.pop()
new_tree = ExpTree(token)
new_tree.insertLeft(left_tree)
new_tree.insertRight(right_tree)
stack.push(new_tree)
else:
if stack.size()2:
raise ValueError("Invalid postfix_expression")
right_tree = stack.pop()
left_tree = stack.pop()
new_tree = ExpTree(token)
new_tree.insertLeft(left_tree)
new_tree.insertRight(right_tree)
stack.push(new_tree)
if stack.size()!=1:
raise ValueError("Invalid postfix_expression")
return stack.pop()
@staticmethod
def inorder(tree):
if tree:
if tree.leftChild and tree.rightChild:
return f'({ExpTree.inorder(tree.leftChild)}{tree.key}{ExpTree.inorder(tree.rightChild)})'
else:
return tree.key
@staticmethod
def postorder(tree):
if tree:
if tree.leftChild and tree.rightChild:
return f'{ExpTree.postorder(tree.leftChild)}{ExpTree.postorder(tree.rightChild)}{tree.key}'
else:
return f'{tree.key}'
@staticmethod
def preorder(tree):
if tree:
if tree.leftChild and tree.rightChild:
return f'{tree.key}{ExpTree.preorder(tree.leftChild)}{ExpTree.preorder(tree.rightChild)}'
else:
return f'{tree.key}'
def evaluate(self):
if isinstance(self.key, ExpTree):
return self.key.evaluate()
elif isinstance(self.key, int) or isinstance(self.key, float):
return float(self.key)
else:
left_val = self.leftChild.evaluate() if self.leftChild else 0
right_val = self.rightChild.evaluate() if self.rightChild else 0
if self.key =='+':
return left_val + right_val
elif self.key =='-':
return left_val - right_val
elif self.key =='*':
return left_val * right_val
elif self.key =='/':
return left_val / right_val
elif self.key =='^':
return left_val ** right_val
def __str__(self):
if self.leftChild and self.rightChild:
return f'({str(self.leftChild)}{self.key}{str(self.rightChild)})'
else:
return self.key
I dont understand how my code does not pass the exptree test conditions
# test an ExpTree
try:
postfix ='523*+'.split()
tree = ExpTree.make_tree(postfix)
print(str(tree))
assert str(tree)=='(5+(2*3))'
score3+=2
except Exception:
pass
try:
assert ExpTree.inorder(tree)=='(5+(2*3))'
assert ExpTree.postorder(tree)=='523*+'
assert ExpTree.preorder(tree)=='+5*23'
score3+=2
except Exception:
pass
try:
assert ExpTree.evaluate(tree)==11.0
score3+=2
except Exception:
pass
try:
postfix ='52+3*'.split()
tree = ExpTree.make_tree(postfix)
assert str(tree)=='((5+2)*3)'
assert ExpTree.inorder(tree)=='((5+2)
image text in transcribed

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 Systems For Advanced Applications 27th International Conference Dasfaa 2022 Virtual Event April 11 14 2022 Proceedings Part 2 Lncs 13246

Authors: Arnab Bhattacharya ,Janice Lee Mong Li ,Divyakant Agrawal ,P. Krishna Reddy ,Mukesh Mohania ,Anirban Mondal ,Vikram Goyal ,Rage Uday Kiran

1st Edition

3031001257, 978-3031001253

More Books

Students also viewed these Databases questions