0

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;
    }

}
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – ΦXocę 웃 Пepeúpa ツ Sep 17 '16 at 11:06
  • this is wrong: ***if (c == "+" || c == "-" || c == "*" || c == "/") {*** – ΦXocę 웃 Пepeúpa ツ Sep 17 '16 at 11:06
  • Should it be char? Thats what i had before, just changed it to String to see if I can insert it to the Node... Still didnt work. Would you happen to know how I can plug each character of my string into a node? – JavaNoob Sep 17 '16 at 11:10
  • it doesnt mattter if its a char or string... you need to compare them right – ΦXocę 웃 Пepeúpa ツ Sep 17 '16 at 11:28
  • This seems wrong too: `n1 = postfix[i];`. `postfix[i]` is a `String` while `n1` is a `Node`, so you cannot assign a `String` to it. – Ole V.V. Sep 17 '16 at 11:59
  • As far as I can tell, you can never create any `OperandNode` instance since the constructor is private and never called from within the class. – Ole V.V. Sep 17 '16 at 12:01
  • We’re sort of trying to guess what you mean by “But everytime I try that is I get an error.” If you told us which error you meant, it would be a lot easier to guide you towards a solution. – Ole V.V. Sep 17 '16 at 12:02
  • See [How do I ask a good question](http://stackoverflow.com/help/how-to-ask). – Ole V.V. Sep 17 '16 at 12:04
  • Thanks! I figured to not make the constructor private and i got a loop iterating through the string and adding the data into the nodes. – JavaNoob Sep 17 '16 at 14:38
  • When do i use the Node interface? Wouldnt i just need to create OperaterNodes and OperandNodes? – JavaNoob Sep 17 '16 at 14:40

0 Answers0