Question: Write a class that transforms a Postfix expression into an expression tree, and provides methods that format the tree in different ways. Any nondouble input

Write a class that transforms a Postfix expression into an expression tree, and provides methods that format the tree in different ways.

Write a class that transforms a Postfix expression into an expression tree,

Any nondouble input read using a Scanners next() method should be considered an operator. Required methods The methods should behave as follows: 1. loadPostfix(String p) should use a Scanner to identify double values. All non-double words should be treated as operators. 2. infix() should return a String containing a parenthesized inorder traversal of the tree. 3. prefix() should return a function-call style preorder traversal of the tree. 4. height() should return the height of the expression tree. None of these methods should print anything out.

Demonstration:

input: 1 2 PLUS infix: (1.0 PLUS 2.0) prefix: PLUS(1.0,2.0) height: 1 input: 1 2 + 3 4 - * infix: ((1.0 + 2.0) * (3.0 - 4.0)) prefix: *(+(1.0,2.0),-(3.0,4.0)) height: 2 input: 4 5 ADD 8.6 33 TIMES MINUS infix: ((4.0 ADD 5.0) MINUS (8.6 TIMES 33.0)) prefix: MINUS(ADD(4.0,5.0),TIMES(8.6,33.0)) height: 2 input: 1 1 1 1 1 1 1 1 * + - FOO PROCESS / X infix: (1.0 X (1.0 / (1.0 PROCESS (1.0 FOO (1.0 - (1.0 + (1.0 * 1.0))))))) prefix: X(1.0,/(1.0,PROCESS(1.0,FOO(1.0,-(1.0,+(1.0,*(1.0,1.0))))))) height: 7 Notice that the infix strings include parentheses. prefix strings do not have any spaces. double values are printed using the default format.

public class ETree ( public void loadPostfix(String postfix); public String infix); public String prefix): public int height()

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!