Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*Java or C++* The goal of this homework is to build an arithmetic expression TRE

ID: 3856245 • Letter: #

Question

*Java or C++*

The goal of this homework is to build an arithmetic expression TREE (not a stack) for a supplied (hard coded) formula, then traverse the tree to evaluate the expression numerically.

Please use the expression used as an example in slides: A * (B + C) / D

You can use infix, prefix, or postfix notation for the formula. I’d suggest using postfix so you can use the tree creation algorithm as described in the slides. The postfix version of the formula is: A B C + * D /

Once you have the tree generated, give the variables integer values for A, B, C, and D, and traverse the tree to calculate the result. There is no need to prompt the user for input, though you can if you want.

For example, if you assign A=2, B=7, C=3, D=5, your result would be 4: 2 * (7 + 3) / 5 = 4

Explanation / Answer

{class Node{

public char data;

public Node (char x)

{ data = x;}

public void display() {

System.out.println(data); } }

class stack1{

private Node a[];

private int top,m;

public stack1(int max){

m= max;

a=new Node[m];

top=-1; }

public void push(Node key) {

a[++top]=key; }.

public Boolean isempty(){

return (top == -1) }

public Node pop( ) { return ( a[top --];}}

class stack2{

private char a[];

private int top,m;   

public stack2(int max){m=max;

a=new char[m];

top=-1;}

public boolean is empty(){ return(top==-1);}.

public void push(char x)

{a[top ++]=x;}

Public void pop(){

return a[top--];

}class conversion { private stack2;

private String in;

private String out;

public conversion(String str){

in =str;

s= new stack2(str.length()); }.

public String postfix(){

for(int i =0; I<= str.lemgth; i ++){

ch= str.charAt(i);

switch(ch){ case '+' :

case ' -' :

getoper(ch