所以我试着建立这个树。我有我的界面Node,现在我正在尝试将节点添加到树中。这是我到目前为止所拥有的。每次我遍历我的字符串并找到一个数字我想把它放入一个节点,然后将该节点添加到我的树。但每次我尝试这是我得到一个错误。请帮忙。
import java.util.ArrayList;
import java.util.Stack;
import javax.swing.tree.TreeNode;
import java.lang.*;
public class BinaryExpressionTree {
boolean isOperator(String c) {
if (c == "+" || c == "-" || c == "*" || c == "/") {
return true;
}
return false;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public void constructTree(String postfix[]) {
Stack<Node> stackTree = new Stack<>();
Node<String> n1;
Node n2;
Node n3;
for(int i = 0; i <= postfix.length; i++){
if(!isOperator(postfix[i])) {
n1 = postfix[i];
}
}else {
}
}
}
以下是我的Node界面的外观: 我还创建了实现它的OperandNode和OperatorNode。
public interface Node<T> {
public T GetData() ;
public void SetLeft(Node Node);
public Node GetLeft();
public void SetRight(Node Node);
public Node GetRight();
}
OperandNode类
public class OperandNode implements Node<Integer> {
int operandData;
Node leftNode;
Node rightNode;
private OperandNode(int operandData, Node leftNode, Node rightNode){
this.operandData = operandData;
this.leftNode = leftNode;
this.rightNode = rightNode;
}
public Integer GetData() {
return operandData;
}
public void SetLeft(Node leftNode) {
this.leftNode = leftNode;
}
public Node GetLeft() {
return leftNode;
}
public void SetRight(Node rightNode) {
this.rightNode = rightNode;
}
public Node GetRight() {
return rightNode;
}
}