subject

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:
If the current token is a left parenthesis add a new node as the left child of the current node. Push current node on the stack and make current node equal to the left child.
If the current token is an operator set the current node's data value to the operator. Push current node on the stack. Add a new node as the right child of the current node and make the current node equal to the right child.
If the current token is an operand, set the current node's data value to the operand and make the current node equal to the parent by popping the stack.
If the current token is a right parenthesis make the current node equal to the parent node by popping the stack if it is not empty.
For the input expression, this is what your program will output:
( ( 8 + 3 ) * ( 7 - 2 ) ) = 55
Prefix Expression: * + 8 3 - 7 2
Postfix Expression: 8 3 + 7 2 - *

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 19:30
When using a public computer or network, you should always
Answers: 2
question
Computers and Technology, 22.06.2019 23:50
List a few alternative options and input and output over the standerd keyboard and monitor. explain their functioning in details.
Answers: 2
question
Computers and Technology, 23.06.2019 04:40
The narrative structure of the popular movies can be broken down into
Answers: 3
question
Computers and Technology, 23.06.2019 18:00
Ramona enjoys her job because she is able to kids in an after school program. the work value ramona feels strongest about is a. leadership b. risk c. independence d. work with people select the best answer from the choices provided a b c d
Answers: 1
You know the right answer?
For this assignment you will read a file expression. txt and create an expression tree. The expressi...
Questions
question
Mathematics, 02.04.2020 11:03
question
Mathematics, 02.04.2020 11:04
question
Mathematics, 02.04.2020 11:05
question
Mathematics, 02.04.2020 11:07
question
Mathematics, 02.04.2020 11:07
question
Mathematics, 02.04.2020 11:08
question
Chemistry, 02.04.2020 11:08
Questions on the website: 13722359