Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the following expression tree in JAVA that works with an expression that contains only variables or double values and supports operations +, -, *,

Implement the following expression tree in JAVA that works with an expression that contains only variables or double values and supports operations +, -, *, and / by providing two additional methods: void setVariable(String name, double value) set the variable name with a value; void displayPostfix() output the ET in postfix format in one line, each token is separated by one space.

package TreePackage; public class ExpressionTree extends BinaryTree implements ExpressionTreeInterface { public ExpressionTree() { } // end default constructor

public double evaluate() { return evaluate(getRootNode()); } // end evaluate

private double evaluate(BinaryNodeInterface rootNode) { double result; if (rootNode == null) result = 0; else if (rootNode.isLeaf()) { String variable = rootNode.getData(); result = getValueOf(variable); } else { double firstOperand = evaluate(rootNode.getLeftChild()); double secondOperand = evaluate(rootNode.getRightChild()); String operator = rootNode.getData(); result = compute(operator, firstOperand, secondOperand); } // end if

return result; } // end evaluate

private double getValueOf(String variable) { // Strings allow multicharacter variables double result = 0; // < To be defined. >

return result; } // end getValueOf private double compute(String operator, double firstOperand, double secondOperand) { double result = 0; // < To be defined. > return result; } // end compute } // end ExpressionTree

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions