Only worried about getting the first test done in the Test Class since the root of the tree is an operator "+" and its are operands/"children" are 3 and 4. Since the root is "+" I want to pop the left child and right child and push the node to the stack. Trying to understand why I cannot use the pop() method from the Stack class.
只担心在测试类中完成第一次测试,因为树的根是一个运算符“+”,它的操作数/“子”是3和4.因为根是“+”我想要弹出左边子项和右项并将节点推送到堆栈。试图理解为什么我不能使用Stack类中的pop()方法。
Node Class
public class Node < E > {
E data;
Node < E > left;
Node < E > right;
public Node(E data) {
this.data = data;
}
public Node(E data, Node < E > left, Node < E > right) {
this.data = data;
this.left = left;
this.right = right;
}
public String toString() {
return data.toString();
}
}
ExpressionTree Class import java.util.Stack;
ExpressionTree类导入java.util.Stack;
public class ExpressionTree {
Node < String > root;
public void buildTree(String expression) {
Stack < Node < String >> s = new Stack < Node < String >> ();
String expArray[] = expression.split(" ");
for (String st: expArray) {
switch (st) {
case "+":
case "-":
case "*":
case "/":
Node < String > right = s.pop();
s.push((new Node < String > (st, s.pop(), right)));
break;
default:
s.push(new Node < String > (st));
}
}
root = s.pop();
}
public void printExpression() {
printExpression(root);
System.out.println();
}
private void printExpression(Node < String > n) {
if (n != null) {
printExpression(n.left);
System.out.print(n);
printExpression(n.right);
}
}
public int evaluateExpression() {
return evaluateExpression(root);
}
public int evaluateExpression(Node < String > n) {
Stack < Node < String >> s = new Stack < Node < String >> ();
n = root;
if (n == null) {
return 0;
} else {
if (n.data.equals("+")) {
s.pop(n.left);
s.pop(n.right);
s.push(n);
evaluateExpression(n);
}
}
return 0;
}
}
Test Class
public class ExpressionTreeTest {
public static void main(String[] args) {
ExpressionTree et = new ExpressionTree();
et.buildTree("3 4 +"); //infix: 3 + 4
et.printExpression();
System.out.println(et.evaluateExpression());
/*et.buildTree("3 4 2 * 1 5 - / +"); //infix: 3+4*2/(1-5)
et.printExpression();
System.out.println(et.evaluateExpression());
et.buildTree("3 4 5 * 2 / +"); //infix: 3+4*5/2
et.printExpression();
System.out.println(et.evaluateExpression());
et.buildTree("12 8 + 6 5 - * 3 2 - 2 3 + * /"); //infix: (12+8)*(6-
5)/((3-2)*(2+3))
et.printExpression();
System.out.println(et.evaluateExpression());*/
}
}
1 个解决方案
#1
1
As of the code
截至代码
s.pop(n.left);
s.pop(n.right);
As the Stack API shows, the pop function does NOT take any parameter.
正如Stack API所示,pop函数不接受任何参数。
It "[r]emoves the object at the top of this stack and returns that object as the value of this function."
它“[r]移动该堆栈顶部的对象,并将该对象作为此函数的值返回。”
You should use another class if you want to remove a specific object instead of whatever is on the top of the stack.
如果要删除特定对象而不是堆栈顶部的任何对象,则应使用另一个类。
#1
1
As of the code
截至代码
s.pop(n.left);
s.pop(n.right);
As the Stack API shows, the pop function does NOT take any parameter.
正如Stack API所示,pop函数不接受任何参数。
It "[r]emoves the object at the top of this stack and returns that object as the value of this function."
它“[r]移动该堆栈顶部的对象,并将该对象作为此函数的值返回。”
You should use another class if you want to remove a specific object instead of whatever is on the top of the stack.
如果要删除特定对象而不是堆栈顶部的任何对象,则应使用另一个类。