Menu

(Solved) : Included Bxtjava Needs Edits Create Binary Expression Tree Also Included Bxtdriver Treenod Q28381400 . . .

I have included BXT.java, which needs too be edits to create aBinary Expression Tree. I have also Included BXTDriver and TreeNodewith this tread.

Instructions:

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 Prefixorder: / 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 Infixorder: 2 + 3 / 5 * 4 – 5 Prefix order: * / + 2 3 5 – 4 5 Evaluatesto -1.0 ————————

Build a BXT You need to change the string into atree. Hint1: In a postfix string the operator is preceded by twooperands (numbers). This suggests a stack of TreeNodes would beuseful. Hint 2: Each operator is a TreeNode with two children. Hint3: If the token is an operator, do what? Else it’s a number, so dowhat? Display Infix and Prefix ordersInfix is characterized by the placement ofoperators between operands; Prefix expressionnotation requires that all operators precede the two operands thatthey work on; Postfix requires that its operatorscome after the corresponding operands. See following examples:Infix Expression Prefix Expression Postfix Expression A + B + A B AB + A + B * C + A * B C A B C * + Evaluating theExpression Do this recursively. If the node is anoperator, recursively evaluate the left child and the right child,and return the result. Else the node is a number, so it can beconverted into a double, and returned. Want to do more?(Optional) Print the infix order with parentheses.

BXT.java :

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 prefixstring and a postfix.  
**********************/
class BXT{
private int count; //will be used in another assignment if we havetime
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
}

}

BXTDriver.java :

public class BXTDriver{
public static void main(String[] args){
ArrayList<String> postExp = newArrayList<String>();
postExp.add(“14 -5 / “);
postExp.add(“20 3 -4 + * “);
postExp.add(“2 3 + 5 / 4 5 – *”);

for( String postfix : postExp ){
System.out.println(“Postfix Exp: ” + postfix);
BXT tree = new BXT();
tree.buildTree( postfix );
System.out.println(“BXT: “);
tree.display();
System.out.print(“Infix order: “);
System.out.println( tree.infix());
System.out.print(“nPrefix order: “);
System.out.println( tree.prefix());
System.out.print(“Postfix order: “);
System.out.println( tree.postfix() );
System.out.print(“nEvaluates to ” + tree.evaluateTree());
System.out.println( “n————————“);
}
}
}

TreeNode.java :

public class TreeNode{
private Object value;
private TreeNode left, right;

public TreeNode(Object initValue){
value = initValue;
left = null;
right = null;
}

public TreeNode(Object initValue, TreeNode initLeft, TreeNodeinitRight){
value = initValue;
left = initLeft;
right = initRight;
}

public Object getValue(){
return value;
}

public TreeNode getLeft() {
return left;
}

public TreeNode getRight(){
return right;
}

public void setValue(Object theNewValue){
value = theNewValue;
}

public void setLeft(TreeNode theNewLeft){
left = theNewLeft;
}

public void setRight(TreeNode theNewRight){
right = theNewRight;
}
}

Expert Answer


Answer to Included Bxtjava Needs Edits Create Binary Expression Tree Also Included Bxtdriver Treenod Q28381400 . . .

OR