Compilation failed Compilation failed zyLabs UnitTest.java:35: error: cannot find symbol if (Util.equals (util.getRoot(), bxt.getRoot ( ) ) ) { symbol: method getRoot () location: variable bxt of type BXT zyLabs UnitTest.java:39: error: cannot find symbol new PrintTree() .print (bxt.getRoot () ); symbol: method getRoot () location: variable bxt of type BXT 2 errors 2:Unit test Compilation failed Compilation failed zyLabs UnitTest.java:16: error: cannot find symbol if (Util.equals (util.getRoot(), bxt.getRoot())) { symbol: method getRoot () location: variable bxt of type BXT zyLabs UnitTest.java:20: error: cannot find symbol symbol: new PrintTree() .print (bxt.getRoot () ); method getRoot () location: variable bxt of type BXT 2 errors
I need help fixing this Java code to output as it's explained in the image below:
import java.util.Stack;
public class BXT {
private TreeNode root;
public void buildTree(String postfix) {
Stack<TreeNode> stack = new Stack<>();
String[] tokens = postfix.split(" ");
for (String token : tokens) {
if (isOperator(token)) {
TreeNode right = stack.pop();
TreeNode left = stack.pop();
stack.push(new TreeNode(token, left, right));
} else {
stack.push(new TreeNode(token));
}
}
root = stack.pop();
}
public double evaluateTree() {
return evaluateTree(root);
}
private double evaluateTree(TreeNode node) {
if (isOperator(node.getData())) {
double left = evaluateTree(node.getLeft());
double right = evaluateTree(node.getRight());
switch (node.getData()) {
case "+": return left + right;
case "-": return left - right;
case "*": return left * right;
case "/": return left / right;
default: throw new IllegalArgumentException("Invalid operator: " + node.getData());
}
} else {
return Double.parseDouble(node.getData());
}
}
public void infix() {
infix(root);
System.out.println();
}
private void infix(TreeNode node) {
if (node != null) {
infix(node.getLeft());
System.out.print(node.getData() + " ");
infix(node.getRight());
}
}
public void prefix() {
prefix(root);
System.out.println();
}
private void prefix(TreeNode node) {
if (node != null) {
System.out.print(node.getData() + " ");
prefix(node.getLeft());
prefix(node.getRight());
}
}
public void postfix() {
postfix(root);
System.out.println();
}
private void postfix(TreeNode node) {
if (node != null) {
postfix(node.getLeft());
postfix(node.getRight());
System.out.print(node.getData() + " ");
}
}
private boolean isOperator(String token) {
return token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/");
}
}
//These codes are involved with the above code that needs fixing
NodeInterface.java (Don't edit)
interface NodeInterface<T>{
T getData();
NodeInterface<T> getLeft();
NodeInterface<T> getRight();
}
/**
*
* TreeNode.java: TreeNode class. // (Don't edit)
*/
public class TreeNode implements NodeInterface<String>
{
private String value;
private TreeNode left, right;
public TreeNode(String initValue)
{
value = initValue;
left = null;
right = null;
}
public TreeNode(String initValue, TreeNode initLeft, TreeNode initRight)
{
value = initValue;
left = initLeft;
right = initRight;
}
public String getData()
{
return value;
}
public TreeNode getLeft()
{
return left;
}
public TreeNode getRight()
{
return right;
}
public void setValue(String theNewValue)
{
value = theNewValue;
}
public void setLeft(TreeNode theNewLeft)
{
left = theNewLeft;
}
public void setRight(TreeNode theNewRight)
{
right = theNewRight;
}
}


Step by step
Solved in 2 steps









