Java 有人能告诉我为什么在我的evaulateExpression中使用pop()不起作用吗?
只担心在测试类中完成第一个测试,因为树的根是运算符“+”,它的are操作数/“children”是3和4。由于根是“+”,我想弹出左子节点和右子节点,并将节点推到堆栈中。试图理解为什么我不能使用Stack类中的pop()方法 节点类Java 有人能告诉我为什么在我的evaulateExpression中使用pop()不起作用吗?,java,data-structures,expression-trees,postorder,Java,Data Structures,Expression Trees,Postorder,只担心在测试类中完成第一个测试,因为树的根是运算符“+”,它的are操作数/“children”是3和4。由于根是“+”,我想弹出左子节点和右子节点,并将节点推到堆栈中。试图理解为什么我不能使用Stack类中的pop()方法 节点类 public class Node < E > { E data; Node < E > left; Node < E > right; public Node(E data) { this.data = data;
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();
}
}
从《守则》开始
s.pop(n.left);
s.pop(n.right);
如图所示,pop函数不接受任何参数
它“[r]移动此堆栈顶部的对象,并将该对象作为此函数的值返回。”
如果要删除特定对象而不是堆栈顶部的任何对象,则应使用另一个类。既然已经有了树,为什么还要使用
stack
?而且,n=rootevaluateExpression()
中的code>丢弃参数的值。。。
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());*/
}
}
s.pop(n.left);
s.pop(n.right);