Question: Expression trees are a type of binary tree that can be used to store print and evaluate mathematical expressions. For this assignment, you will implement
Expression trees are a type of binary tree that can be used to store print and evaluate mathematical expressions.
For this assignment, you will implement an ExpressionTree Class. The ExpressionTree class will need to be able to parse a mathematical expression, load it into a binary tree, print the expression in Postfix or Infix notation, and evaluate the expression. You should also write code to test your ExpressionTree class or use the provided test code: ExpressionTreeTest.cpp Download ExpressionTreeTest.cpp
ExpressionTree Class
Must support the following public methods:
ExpressionTree default constructor creates an empty expression tree
ExpressionTreestd::string expr Creates an expression tree from the given string expression
setExpressionstd::string expr Clears the expression tree and stores the given expression in a new tree
getResult return the results of evaluating the expression tree
printParseTreeInOrderstd::ostream& outputs the tree to the ostream& using an inorder traversal
printParseTreePostOrderstd::ostream& outputs the tree to the ostream& using postorder traversal
Parsing Building and Evaluating ExpressionTrees
The core of this assignment revolves around takExpression trees are a type of binary tree that can be used to store print and evaluate mathematical expressions.
For this assignment, you will implement an ExpressionTree Class. The ExpressionTree class will need to be able to parse a mathematical expression, load it into a binary tree, print the expression in Postfix or Infix notation, and evaluate the expression. You should also write code to test your ExpressionTree class or use the provided test code: ExpressionTreeTest.cpp Download ExpressionTreeTest.cpp
ExpressionTree Class
Must support the following public methods:
ExpressionTree default constructor creates an empty expression treeExpressionTreestd::string expr Creates an expression tree from the given string expressionsetExpressionstd::string expr Clears the expression tree and stores the given expression in a new treegetResult return the results of evaluating the expression treeprintParseTreeInOrderstd::ostream& outputs the tree to the ostream& using an inorder traversalprintParseTreePostOrderstd::ostream& outputs the tree to the ostream& using postorder traversal
Parsing Building and Evaluating ExpressionTrees
The core of this assignment revolves around taking a statement like: and converting it into a tree like this:
Building the expression tree can be done recursively like follows:
If the character is a create a node and place it on the left of your currentNode, move to the next character in the expression and recursively go left from the currentNode.
if the character is a digit, continue reading from the string until you encounter a nondigit character. Each time concatenate the new digit character into a temporary string. Store that temp string in your currentNode data and then return.
If the character is an operator store the operator in the currentNode data, create a new node and place it on the right of your currentNode, move to the next character in the expression and recursively go right from the currentNode.
if the character is a just return.
Evaluating the expression tree can also be done recursively like follows:
If the node is null the result is
If the node is a number the result is that number
If the node is an operator the result is the recursive result of the left subtree and right subtree combined using the given operator
My is code in C
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
