For this assignment you will read a file expression.txt and create an expression tree. The expression will be a valid infix expression with the all the necessary parentheses so that there is no ambiguity in the order of the expression. You will evaluate the expression and print the result. You will also write the prefix and postfix versions of the same expression without any parentheses.
In an expression tree the nodes are either operators or operands. The operators will be in the set ['+', '-', '*', '/']. The operands will be either integers or floating point numbers. All the operand nodes will be leaves of the expression tree. All the operator nodes will have exactly two children.
The outline of your program will be as follows:
class Stack (object): class Node (object): class Tree (object): def __init__ (self): def createTree (self, expr): def evaluate (self, aNode): def preOrder (self, aNode): def postOrder (self, aNode): def main(): main()
The function createTree() will take as input parameter an infix expression with parentheses as a String and create an Expression Tree from it. Assume that the expression string is valid.
You will take the expression string and break it into tokens. There are four different kinds of tokens - left parenthesis, right parenthesis, operator, and operand. When we read a left parenthesis we are starting a new expression and when we read a right parenthesis we are ending an expression. Here is the algorithm that you will use:
For the input file this is what your program will output:
( ( 8 + 3 ) * ( 7 - 2 ) ) = 55 Prefix Expression: * + 8 3 - 7 2 Postfix Expression: 8 3 + 7 2 - *
The file that you will be turning in will be called ExpressionTree.py. The file will have a header of the following form:
# File: ExpressionTree.py # Description: # Student's Name: # Student's UT EID: # Course Name: CS 313E # Unique Number: 53260 # Date Created: # Date Last Modified:
Use the turnin program to submit your ExpressionTree.py file. The TAs should receive your work by 11 PM on Friday, 12 April 2013. We will be looking for clean logic and good documentation. There will be substantial penalties if you do not adhere to the guidelines.