Question

Note: Once completed, run BXTDriver.class, then record the output to result.txt under your assignment folder. Without...

Note: Once completed, run BXTDriver.class, then record the output to result.txt under your assignment folder. Without result.txt, you will receive zero(0) for this assignment.

For this assignment, we will build a binary expression tree, display it, and evaluate it.   This time, we will encapsulate the behavior in a BXT class.   The driver class, tree node, as well as the BXT class are provided. Please implement appropriate methods in BXT class to build, display and evaluate a tree. 

Let's also allow decimal input and output, and negative numbers.  For simplicity, all tokens in the input string be separated by a space.

Sample run

Postfix Exp: 14 -5 / BXT: -5 / 14 Infix order: 14 / -5 Prefix order: / 14 -5 Evaluates to -2.8 ------------------------ Postfix Exp: 20 3 -4 + * BXT: -4 + 3 * 20 Infix order: 20 * 3 + -4 Prefix order: * 20 + 3 -4 Evaluates to -20.0 ------------------------ Postfix Exp: 2 3 + 5 / 4 5 - * BXT: 5 - 4 * 5 / 3 + 2 Infix order: 2 + 3 / 5 * 4 - 5 Prefix order: * / + 2 3 5 - 4 5 Evaluates to -1.0 ------------------------


Build a BXT You need to change the string into a tree. Hint1: In a postfix string the operator is preceded by two operands (numbers). This suggests a stack of TreeNodes would be useful. Hint 2: Each operator is a TreeNode with two children. Hint 3: If the token is an operator, do what? Else it's a number, so do what? Display Infix and Prefix orders Infix is characterized by the placement of operators between operands; Prefix expression notation requires that all operators precede the two operands that they work on; Postfix requires that its operators come after the corresponding operands. See following examples: Infix Expression Prefix Expression Postfix Expression A + B + A B A B + A + B * C + A * B C A B C * + Evaluating the Expression Do this recursively. If the node is an operator, recursively evaluate the left child and the right child, and return the result. Else the node is a number, so it can be converted into a double, and returned. Want to do more? (Optional) Print the infix order with parenthese

/** INSTRUCTION
* Create a class named BXT.java the copy and past the content
* Once completed, please record how long time you spent on this assignment
* and list all resources you have used to complete this assignment.
*/
import java.util.*;
  
/*******************
Represents a binary expression tree.
The BXT can build itself from a postorder expression. It can
evaluate and print itself. It also prints an infix string, a prefix string and a postfix.
**********************/
class BXT{
private int count; //will be used in another assignment if we have time
private TreeNode root;
public BXT(){
count = 0;
root = null;
}
public void buildTree(String str){
//TO DO
}
public double evaluateTree(){
//To DO
}

public String display(){
//TO DO
}

public String infix(){
//TO DO
}

public String prefix(){
//TO DO
}

public String postfix(){
//TO DO
}

}

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

Program Screenshot:

BXT.java

Add a comment
Know the answer?
Add Answer to:
Note: Once completed, run BXTDriver.class, then record the output to result.txt under your assignment folder. Without...
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
  • Total point: 15 Introduction: For this assignment you have to write a c program that will...

    Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...

  • 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...

  • 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...

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

  • Assignment 4 1)For all the questions in Assignment 4, you need to draw a diagram to...

    Assignment 4 1)For all the questions in Assignment 4, you need to draw a diagram to describe the steps and provide short description as needed. Construct a Binary Search Tree by inserting the following node one by one. [4 points] 7,12,6,9,11,23,24,27,15 2)Provide an example for each following cases to search a given key in a Binary Search Tree. [4 points] Time efficiency=Ο(1)3S8CEMtd9AAAAAElFTkSuQmCC Time efficiency=ΟlogncdA3f5imtB2VefcxtquYZA8cQHOJLX+AydvU9UjZ Time efficiency=Ο(n)ZRO+N+OcdxOBP4CbNI+pPm7zwkAAAAASUVORK5CY 3)For the BST you created in question 1, delete the node 9, then 7,...

  • Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a...

    Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands. The algorithm to evaluate any postfix expression is based on stack and is pretty simple: Initialize empty stack For every token in the postfix expression (scanned from left to right): If the token is an operand (number), push it on...

  • Assignment 4 1)For all the questions in Assignment 4, you need to draw a diagram to describe the ...

    Assignment 4 1)For all the questions in Assignment 4, you need to draw a diagram to describe the steps and provide short description as needed. Construct a Binary Search Tree by inserting the following node one by one. [4 points] 7,12,6,9,11,23,24,27,15 2)Provide an example for each following cases to search a given key in a Binary Search Tree. [4 points] Time efficiency=Ο(1)3S8CEMtd9AAAAAElFTkSuQmCC Time efficiency=ΟlogncdA3f5imtB2VefcxtquYZA8cQHOJLX+AydvU9UjZ Time efficiency=Ο(n)ZRO+N+OcdxOBP4CbNI+pPm7zwkAAAAASUVORK5CY 3)For the BST you created in question 1, delete the node 9, then 7,...

  • Using ADT Stack: Evaluating infix expressions by converting them to postfix expressions Postfix notation: In a...

    Using ADT Stack: Evaluating infix expressions by converting them to postfix expressions Postfix notation: In a postfix expression, a binary operation follows its two opperands. The order of the operands in a infix expression is the same as in the corresponding postfix expression but the order of the operators might change based on the precedence of the operators and the existing of paranthses. Infix Postfix a + b a b + (a + b) * c a b + c...

  • 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...

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

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