Question

construct an expression tree, then print the preorder of the tree for each expression Write this...

construct an expression tree, then print the preorder of the tree for each expression

Write this in Java, test all cases below

7 * ( 3 - 6 ) + 5 => + * 7 - 3 6 5 13.0 +

18 / -4 => + 13.0 / 18 -4

0.11 / 5 * ( 0.2 * 7 ) => * / 0.11 5 * 0.2 7

-0.27 * 2.21 + 3 - -0.7 / -2

2.05 - 5 + 3 -3 - 1 + 2 + -3.4 / 5

( 855 / 11 ) * ( 12.2 / 321 - ( 1 + 2 + ( 1 + 1.1 + ( 12.51 - 0.51 ) ) ) ) - 0.11

-0.12 + 1.22 - -12.1 / 2.1 => - + -0.12 1.22 / -12.1 2.1

0.5 * 8 / ( 12 + 5 * ( 2 + 3 / 2 ) ) - ( 5 + 1 ) * 3 / 2.5 ==> - / * 0.5 8 + 12 * 5 + 2 / 3 2 / * + 5 1 3 2.5

( -5 + 2 ) / -3 * 2 - -1 / 3 * ( ( 5 / -2) + 1.5 ) => - * / + -5 2 -3 2 * / -1 3 + / 5 -2 1.5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package javaapplication3;
import java.io.*;
class Stack
{
private char[] a;
private int top,m;
public Stack(int max)
{
m=max;
a=new char[m];
top=-1;
}
public void push(char key)
{
a[++top]=key;
}
public char pop()
{
return(a[top--]);
}
public char peek()
{
return(a[top]);
}
public boolean isEmpty()
{
return (top==-1);
}
}
class preoder
{
private Stack s;
private String input;
private String output="";
public preoder(String str)
{
input=str;
s=new Stack(str.length());
}
public String pre()
{
for(int i=input.length()-1;i>=0;i--)
{
char ch=input.charAt(i);
switch(ch)
{
case '+':
case '-':gotOperator(ch,1,')');
break;
case '*':
case '/':gotOperator(ch,2,')');
break;
case ')':s.push(ch);
break;
case '(':gotParenthesis(')');
break;
default:output=ch+output;
}
}
while(!s.isEmpty())
output=s.pop()+output;
return output;
}

private void gotOperator(char opThis,int prec1,char x)
{
while(!s.isEmpty())
{
char opTop=s.pop();
if(opTop==x)
{
s.push(opTop);
break;
}
else
{
int prec2;
if(opTop=='+'||opTop=='-')
prec2=1;
else
prec2=2;
if(prec2<prec1&&x=='(')
{
s.push(opTop);
break;
}
else if(prec2<=prec1&&x==')')
{
s.push(opTop);
break;
}
else
{
if(x==')')
output=opTop+output;
else
output=output+opTop;
}
}
}
s.push(opThis);
}
private void gotParenthesis(char x)
{
while(!s.isEmpty())
{
char ch=s.pop();
if(ch==x)
break;
else
{
if(x==')')
output=ch+output;
else
output=output+ch;
}
}
}
}
class conversion
{
public static void main(String args[])throws IOException
{
String s;
preoder exp;
DataInputStream inp=new DataInputStream(System.in);
  
System.out.println("Enter the infix expression ");
s=inp.readLine();
exp=new preoder(s);
System.out.println("Prefix expression:- "+exp.pre());   
  
}
}

Note: Put proper parenthesis while providing the input

Add a comment
Know the answer?
Add Answer to:
construct an expression tree, then print the preorder of the tree for each expression Write this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT