Implement an Arithmetic Expression display and evaluator For
Implement an Arithmetic Expression display and evaluator.
For all methods except isArithmetic , you may assume that the input is valid arithmetic.
public boolean isArithmetic() // is this tree a valid arithmetic tree
// every leaf in an arithmetic tree is a numeric value, for example: “1”, “2.5”, or “0.234”
// every internal node is a binary operation: “+”, “”, “*”, “/”
// binary operations must act on exactly two subexpressions (i.e. two children)
// note: all the values and operations are stored as String objects public double evaluateArithmetic()
// evaluate an arithmetic tree to get the solution // if a position is a numeric value, then it has that value
// if a position is a binary operation, then apply that operation on the value of it’s children
// use floating point maths for all calculations, not integer maths
// if a position contains “/”, its left subtree evaluated to 1.0, and the right to 2.0, then it is 0.5
public String getArithmeticString()
// Output a String showing the expression in normal (infix) mathematical notation
// For example: “(1+2)+3”
// You must put brackets around every binary expression
// You may omit the brackets from the outermost binary expression
Here is the outline for :-
import java.util.List;
import interfaces.Tree;
import interfaces.TreeArithmetic;
import interfaces.TreeProperties;
import interfaces.TreeTraversals;
import simpletree.SimpleTree;
public class MyTree<E extends Comparable<E>> extends SimpleTree<E> implements
TreeTraversals<E>,
TreeProperties,
Comparable<Tree<E>>,
TreeArithmetic
{
//constructor
public MyTree() {
super(); //call the constructor of SimpleTree with no arguments
}
}
}
outline for tree arithmetic
package interfaces;
public interface TreeArithmetic {
public boolean isArithmetic();
// is this tree a valid arithmetic tree
// every leaf in an arithmetic tree is a numeric value, for example: “1”, “2.5”, or “-0.234”
// every internal node is a binary operation: “+”, “-”, “*”, “/”
// binary operations must act on exactly two sub-expressions (i.e. two children)
public double evaluateArithmetic();
// evaluate an arithmetic tree to get the solution
// if a position is a numeric value, then it has that value
// if a position is a binary operation, then apply that operation on the value of it’s children
// use floating point maths for all calculations, not integer maths
// if a position contains “/”, its left subtree evaluated to 1.0, and the right to 2.0, then it is 0.5
public String getArithmeticString();
// Output a String showing the expression in normal (infix) mathematical notation
// For example: (1 + 2) + 3
// You must put brackets around every binary expression
// You may omit the brackets from the outermost binary expression
}
Solution
step1:public double evaluateArithmetic()
// evaluate an arithmetic tree to get the solution // if a position is a numeric value, then it has that value
// if a position is a binary operation, then apply that operation on the value of it’s children
// use floating point maths for all calculations, not integer maths
// if a position contains “/”, its left subtree evaluated to 1.0, and the right to 2.0, then it is 0.5
step2:public String getArithmeticString();
// Output a String showing the expression in normal (infix) mathematical notation
// For example: (1 + 2) + 3
// You must put brackets around every binary expression
// You may omit the brackets from the outermost binary expression
From above two methods how this Arithmetic Expression display and evaluator is going on we see on below,
In order to calculate an arithmetic expression, we need to know precise order in which operators are executed. Operations are not always performed in the same order, and that is from where most of the troubles are coming. Consider these two expressions: 1.0+2.0+0.5 and 1.0+2.0*0.5.
In the first case, addition operators are executed as they appear, from left to right. In the second case, the order is opposite because multiplication takes precedence over addition. Off course, taking precedence can be prevented by placing parentheses: (1+2)*3.
Everything seems to be fine as long as there are only two operators involved. But observe a little bit more complex expression: 1*(2+6/2)/8. To solve expression of this level of complexity, we need better suited tools. One standard solution to the problem is to build an expression tree. Nodes in the tree are operations (signified by their operators) and branches lead to operands. Leaf nodes in the tree are plain values, and they do not require further evaluation. Here is the tree representing the last expression listed above:
In this way the process in Arithmetic Expression display and evaluator is going.

