Question: Complete the two classes in java: (please show output) 1. ExpressionTree is a class to encapsulate information for an expression tree. Use the values 3.23
Complete the two classes in java: (please show output)
1. ExpressionTree is a class to encapsulate information for an expression tree. Use the values 3.23 for a, 5.45 for b, -4.56 for c, 10.2 for d, -2.33 for e, 2.01 for f, 9.99 for g
2. DriverExpressionTree is a class to test the ExpressionTree class. It first creates an ExpressionTree to represent the expression tree shown below:
You must create an ExpressionTree for each subtree starting with the leaves and working your way up the tree to the root. It will begin like this:
ExpressionTree a = new ExpressionTree("a");
ExpressionTree b = new ExpressionTree("b");
ExpressionTree abPlus = new ExpressionTree();
abPlus.setTree("+", a, b);
and will end with
ExpressionTree myTree = new ExpressionTree();
myTree.setTree(three parameters);
Then the program will evaluate myTree and carry out the two traversals
---------------------DriverExpressionTree.java--------------------
/ This class will test the methods from ExpressionTree // Complete ExpressionTree class first and then // hardcode in the tree from the assignment
import TreePackage.ExpressionTree; import java.util.Iterator;
public class DriverExpressionTree { public static void main(String[] args) { // Build expression tree called myTree // for the tree shown in the assignment // Output evaluation and traversals System.out.println("Expression Tree evaluates to " + myTree.evaluate()); myTree.displayTree(); }
---------------------ExpressionTree.java--------------------
package TreePackage;
// This is a class to represent an expression tree // As noted below, some of the methods are incomplete // You should complete the unfinished methods // Do not change other methods or add any new ones
public class ExpressionTree extends BinaryTree
public ExpressionTree(String rootData) { super(rootData); }
public double evaluate() { return evaluate(getRootNode()); } // complete the recursive method below // method will evaluate an expression tree rooted at rootNode private double evaluate(BinaryNode
if (rootNode == null) result = 0; else if (rootNode.isLeaf()) { // result is the value of the rootNode
} else { // use recursion to calculate result based on the results // of the left subtree and the right subtree and the // operation in rootNode
}
return result; } private double getValueOf(String variable) { // Method returns the value of any possible variable
}
private double compute(String operator, double firstOperand, double secondOperand) { // Calculate the result of the operator on the two operands
}
// For testing purposes. Do not change public void displayTree() { BinaryNode
// Carries out a postorder traversal on the tree rooted at rootNode // Method will print the node's data when it visits the node private void postorder(BinaryNode
} }
// Carries out a preorder traversal on the tree rooted at rootNode // Method will print the node's data when it visits the node private void preorder(BinaryNode
} } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
