Question: import java.util.Stack; public class ExpressionTree { Node root; // Node sub-class public class Node { private String key; // key private Node left, right; //

 import java.util.Stack; public class ExpressionTree { Node root; // Node sub-class

import java.util.Stack;

public class ExpressionTree

{

Node root;

// Node sub-class

public class Node

{

private String key; // key

private Node left, right; // links to subtrees

public Node(String key)

{

this.key = key;

}

}

// constructor to build a tree with postFix expression

public ExpressionTree(String postFixExpression)

{

Stack stack = new Stack();

String[] tokens = postFixExpression.split(" ");

for (String token: tokens)

{

if (isOperator(token))

{

Node right = stack.pop();

Node left = stack.pop();

Node node = new Node(token);

node.left = left;

node.right = right;

stack.push(node);

}

else

{

stack.push(new Node(token));

}

}

root = stack.pop();

}

private boolean isOperator(String token)

{

boolean result = false;

switch(token)

{

case "+":

case "-":

case "*":

case "/":

result = true;

break;

default:

result = false;

}

return result;

}

/**

//NEED HELP HERE

* @return result of the expression

* @throws Exception

*/

public double evaluate() throws Exception

{

//CODE HERE

{

}

8. A binary tree can be used to represent an expression with leaf nodes as operands and other nodes as binary operators, as the following example shows. Implement the following method inside Expression Tree cl only valid operators and operands are double numbers in String format. public double evaluate) throws Exception ass, assuming "+, -, *, /"are the 9 dl

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!