So im trying to build this Tree. I have my interface Node and now I am trying to add the Nodes to the tree. Here is what I have so far. Everytime I iterate through my string and find a Number I want to put that into a Node and then add that node to my tree. But everytime I try that is I get an error. Please help.
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 {
}
}
}
Here is how my Node interface looks: I've also created the OperandNode and OperatorNode that implements it.
public interface Node<T> {
public T GetData() ;
public void SetLeft(Node Node);
public Node GetLeft();
public void SetRight(Node Node);
public Node GetRight();
}
OperandNode class
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;
}
}