Question: Programming Activity 6 - Guidance ================================= Part 1 ------ Note that the LeafNode class already shows the solution for its postfix() method. postfix() simply returns
Programming Activity 6 - Guidance ================================= Part 1 ------ Note that the LeafNode class already shows the solution for its postfix() method. postfix() simply returns a string version of itself. To do this, it calls str on itself. str calls __str__, which returns the _data field converted to a string. The LeafNode infix() method does the same things as postfix(). Part 2 ------ The LeafNode prefix() method does the same things as postfix(). Part 3 ------ The LeafNode value() method simply returns the _data field. Make sure to refer to this field using the provided "self" object reference. Part 4 ------ Note that the InteriorNode class already shows the solution for its postfix() method. postfix() returns a string of the form: postfix of its left operand a space postfix of its right operand a space its operator Note that this pattern results in a recursive descent of the expression tree. The InteriorNode infix() method should use the following similar pattern: a left parenthesis "(" infix of its left operand a space its operator a space infix of its right operand a right parenthesis ")" Part 5 ------ The InteriorNode prefix() method should use the following pattern: its operator a space prefix of its left operand a space prefix of its right operand Part 6 ------ The InteriorNode value() method should use the following steps: Use self._leftOperand.value() to get the value of its left operand. Use self._rightOperand.value() to get the value of its right operand. self._operator contains the operator type. The valid operator types are: "+", "-", "*", "/", and "^". Use self._operator in conditional logic to calculate the value to return. Examples: If the operator is "+" (addition), the returned value should be: value of left operand + value of right operand If the operator is "^" (exponentiation), the returned value should be: value of left operand ** value of right operand If the operator does not equal a valid value, return 0. outcome: Enter an infix expression: 7 - 2 * 5 Prefix: - 7 * 2 5 Infix: (7 - (2 * 5)) Postfix: 7 2 5 * - Value: -3 Enter an infix expression: (7 - 2) * 5 Prefix: * - 7 2 5 Infix: ((7 - 2) * 5) Postfix: 7 2 - 5 * Value: 25 Enter an infix expression: 9 * 5 + 10 Prefix: + * 9 5 10 Infix: ((9 * 5) + 10) Postfix: 9 5 * 10 + Value: 55 Enter an infix expression: 6 + 5 - 2 + 3 Prefix: + - + 6 5 2 3 Infix: (((6 + 5) - 2) + 3) Postfix: 6 5 + 2 - 3 + Value: 12 Enter an infix expression: 5 * 7 ^ 2 Prefix: * 5 ^ 7 2 Infix: (5 * (7 ^ 2)) Postfix: 5 7 2 ^ * Value: 245 Enter an infix expression: (4 * (2 + 3) ^ (2 + 3) Error: Parsing error -- ')' expected Expression so far = (4 * (2 + 3) ^ (2 + 3) Enter an infix expression: 4 * (2 + 3) ^ (2 + 3) Prefix: * 4 ^ + 2 3 + 2 3 Infix: (4 * ((2 + 3) ^ (2 + 3))) Postfix: 4 2 3 + 2 3 + ^ * Value: 12500 Enter an infix expression: >>> expression tree nodes. Source: Textbook chapter 10 Parsing and Expression Trees case study student files. """ class LeafNode(object): """Represents an integer.""" def __init__(self, data): self._data = data def __str__(self): return str(self._data) # Part 1: def infix(self): # return str(self) # Part 2: def prefix(self): # return str(self) def postfix(self): return str(self) # Part 3: def value(self): # return str(self._data) class InteriorNode(object): """Represents an operator and its two operands.""" def __init__(self, op, leftOper, rightOper): self._operator = op self._leftOperand = leftOper self._rightOperand = rightOper # Part 4: def infix(self): # return "(" + self._leftOperand.infix() + " " + \ self._operator + " " + \ self._rightOperand.infix() + ")" # Part 5: def prefix(self): # return self._operator + " " + \ self._leftOperand.prefix() + " " + \ self._rightOperand.prefix() def postfix(self): return self._leftOperand.postfix() + " " + \ self._rightOperand.postfix() + " " + \ self._operator # Part 6: def value(self): # self._leftOperand.value() self._rightOperand.value() self._operator = ( "+" "-" "*" "/" "^" ) if (self._operator == "+" ): return leftOperand + rightOperand elif (self._operator == "^" ): return leftOperad ** rightOperand elif (self._operator != "+" "-" "*" "/" "^" ): return 0 # A simple tester program: def main(): a = LeafNode(4) b = InteriorNode('+', LeafNode(2), LeafNode(3)) c = InteriorNode('*', a, b) c = InteriorNode('^', c, b) print("Expect ((4 * (2 + 3) ^ (2 + 3)):", c.infix()) print("Expect ^ * 4 + 2 3 + 2 3 :", c.prefix()) print("Expect 4 2 3 + * 2 3 + ^ :", c.postfix()) print("Expect 3200000 :", c.value()) if __name__ == "__main__": main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
