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
  • Java construct an expression tree, then print the preorder of the tree for each expression Test...

    Java construct an expression tree, then print the preorder of the tree for each expression Test following cases: 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...

  • Tree and Preorder For each expression, construct an expression tree, then print the preorder of the...

    Tree and Preorder For each expression, construct an expression tree, then print the preorder of the tree. 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...

  • [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree,...

    [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals.You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Pre order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line...

  • (1) (50%) Write a C program that takes as input a fully parenthesized, arithmetic expression of...

    (1) (50%) Write a C program that takes as input a fully parenthesized, arithmetic expression of binary operators +, -,*,/, and converts the expression into a binary expression tree. Your program should take input from the command line. The entire expression should be in a character string without any space in it An input string only includes floating numbers in the format of Y.YY, that is, one digit to the left of the decimal point and two digits to the...

  • Please help calculate average CFU/ml Treatment 1: BHI, 370c, +02 Time (min)oD (average) CFU/ml # of...

    Please help calculate average CFU/ml Treatment 1: BHI, 370c, +02 Time (min)oD (average) CFU/ml # of colonies 65, 118, 119, 13 8, 16, 13, o Dilution 0.11 E-6 E-7 15 30 0.12 0.19 E-6 E-7 97, 155, 213, 122 18, 8, 26, 10 45 60 0.31 0.51 E-6 E-7 224, 273, 156, 258 64, 44, 16, 66 75 90 0.88 52, 67, 96, TNTC 14, 5, 9, 93 0.98 E-7 E-8 105 120 1.07 E-7 E-8 400, 116, TNTC, 0...

  • Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not...

    Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree.    Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...

  • Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are n...

    Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree.    Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...

  • Given the morphological characteristics in the table below, construct a parsimonious tree. Given the distance matrix...

    Given the morphological characteristics in the table below, construct a parsimonious tree. Given the distance matrix in the table below, construct a parsimonious tree. Species 1 Species 2 Species 3 Species 4 Species 5 Species 6 Species 7 Species 1 11 18 2 19 17 3 Species 2 11 17 9 18 19 10 Species 3 18 17 18 2 4 17 Species 4 2 9 18 20 5 4 Species 5 19 18 2 20 7 17 Species 6...

  • Given the distance matrix in the table below, construct a parsimonious tree. Given the distance matrix...

    Given the distance matrix in the table below, construct a parsimonious tree. Given the distance matrix in the table below, construct a parsimonious tree. Species 1 Species 2 Species 3 Species 4 Species 5 Species 6 Species 7 Species 1 11 18 2 19 17 3 Species 2 11 17 9 18 19 10 Species 3 18 17 18 2 4 17 Species 4 2 9 18 20 5 4 Species 5 19 18 2 20 7 17 Species 6...

  • HW Score: 77.78 %, 14 of 18 p Score: 0 of 1 pt 15 of 18...

    HW Score: 77.78 %, 14 of 18 p Score: 0 of 1 pt 15 of 18 (15 complete) X 3.3.22 Question Help Use the following cell phone airport data speeds (Mbps) from a particular network. Find Q3. 0.5 0.4 0.4 0.4 0.5 0.5 0.5 0.2 0.3 0.4 0.9 1.2 1.2 1.3 1.5 0.8 0.8 0.9 0.7 0.7 2.8 3.6 3.6 4.9 2.4 2.4 2.4 1.8 2.1 2.2 7.6 9.5 12.2 8.6 12.1 5.6 6.5 7,4 4.9 5.5 25.3 13.8 14.2...

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