Question

Programming in Java Q: Build an expression tree for a given postfix expression. Then compute the value of the expression by d

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

import java.util.Scanner.*;

interface XYZ{

build_expression_tree(args);

evaluate_expression_tree(args);

}

class ExpressionTree implements XYZ{

static boolean isOperand(char c)

{

    return (c >= '0' && c <= '9');

}

static build_expression_tree( char args)

{

    return (int)(c - '0');

}

static int evaluate_expression_tree(String exp)

{

    if (exp.length() == 0) return -1;

    int res = value(exp.charAt(0));

    for (int i = 1; i<exp.length(); i += 2)

    {

        char opr = exp.charAt(i), opd = exp.charAt(i+1);

        if (isOperand(opd) == false) return -1;

        if (opr == '+') res += value(opd);

        else if (opr == '-') res -= value(opd);

        else if (opr == '*') res *= value(opd);

        else if (opr == '/') res /= value(opd);

        else                

return -1;

    }

    return res;

}

public static void main(String args[]){

    String expr1 = "1 6 * 8+";

    int res = evaluate_expression_tree(expr1);

    if(res == -1) System.out.println(expr1+" is Invalid");

    else     System.out.println("Value of "+expr1+" is "+res);

}

}

Add a comment
Know the answer?
Add Answer to:
Programming in Java Q: Build an expression tree for a given postfix expression. Then compute the ...
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
  • Write a java program to convert and print an infix expression to postfix expression. You can...

    Write a java program to convert and print an infix expression to postfix expression. You can use Java stack methods. (Must read input from System.in) Your main method should be as follow: public static void main(String args[]) { intopost p = new intopost (); String iexp, pexp; //infix postfix expression             try{ Scanner inf = new Scanner (System.in);                     // Read input from KB/ File while(inf.hasNext()){ // read next infix expression                                 iexp = inf.next(); // Assume method name to convert infix...

  • Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user....

    Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user. 2. Evaluate expression using double stack made of your own linked list. 3. Show the result of expression or error if incorrect. Evaluating Postfix Expression Input an expression: 2 10 + 9 6 - / Evaluating Postfix Expression Input an expression: 20 10 + 9 6 - 1 Evaluating Postfix Expression Input an expression: 2 10 + 9 - / Result = 4.0 Result...

  • You are to write a program name expressionTree.java that evaluates an infix expression entered by the...

    You are to write a program name expressionTree.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: (1) Integer constants (a series of decimal digits). (2)   One alphabetic character - "x" (representing a value to be supplied later). (3)   Binary operators (+, -, *, / and % (modulo)). (4)   Parentheses          You will parse the input expression creating an expression tree with the tokens, then use the postOrder tree traversal algorithm to extract...

  • C++ Programming Implement a class ExprTree for representing expression trees, including: 1. The operations to build...

    C++ Programming Implement a class ExprTree for representing expression trees, including: 1. The operations to build an expression tree from a postfix arithmetic expression. 2. A recursive function to "evaluate" a non-empty expression tree using these rules:    - If the tree has only one node (which must be a leaf), then the evaluation of the tree returns the real number (type double) that is the node's entry. - If the tree has more tha one node, and the root...

  • Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expressi...

    Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expression , convert to a postfix expression and finally evaluate the postfix expression… Follow the examples done during class lectures… We are used to infix notation - ”3 + 4” - where the operator is between the operands. There is also prefix notation, where the operand comes before...

  • This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming...

    This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...

  • Can someone help me in java Net Beans IDE witht the code andjava comments. Thanks...

    Can someone help me in java Net Beans IDE witht the code and java comments. ThanksMany of these classes will just need to be copied from previous assignments. When you implement the Tree interface you may import the java.util.Iterator class. When you implement the AbstractBinaryTree class you may import the java.util.ArrayList class and the java.util.List class.In this project you will be doing the following:Create a NetBeans project named lab07 and ensure it is imported into your SVN.In this lab assignment...

  • This is in Java Create a client class that does the following: Manually creates an instance...

    This is in Java Create a client class that does the following: Manually creates an instance of a binary expression tree that represents the following expression: (((5+2)*(2-1)/((2+9))+((7-2)-1))*8) Do this by using methods such as addRoot, addLeft, addRight, set, and attach. The element should be of type String Note that you are manually building this specific expression tree, the pseudo code might look something like: Create a LinkedBinaryTree Add “*” as the root to the tree Add “/” as the left...

  • (In Java) you will be creating a GUI for your calculator that evaluates the postfix expression. I...

    (In Java) you will be creating a GUI for your calculator that evaluates the postfix expression. I have already created the postfix class that converts the expression from infix to postfix. Here's the postFix class and its stack class (https://paste.ofcode.org/bkwPyCMEBASXQL4pR2ym43) ---> PostFix class (https://paste.ofcode.org/WsEHHugXB38aziWRrp829n)--------> Stack class Your calculator should have: Text field for the postfix expression Text field for the result Two buttons: Evaluate and Clear You can start with the code written below and improvise to work with this...

  • Objective To acquire expertise in stack manipulation and management, subroutine linkage and retur...

    Objective To acquire expertise in stack manipulation and management, subroutine linkage and return conventions, and recursive procedures. Description You are to create a MIPS programming assignment that returns postfix representation of the input and create an expression tree. Your MIPS program should make use of the expression tree to store the input and provide, by means of an adequate traversal, the value for the expression. Your solution should be structured according to the following steps: Step 1: Convert expression from...

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