title: toString causing StackOverflow error
body: <p>I overrode my toString() in my Node class for a BinaryTree. Now it causes a StackOverflow and I cant figure out why.  </p>
  
  <p>My BinaryTree.Java file isn't complete, but it works for what iv completed up to this point.  </p>
  
  <p>So basically, if I include the overridden toString() in Node.java my code breaks. and I'm lost as to why. I was thinking somehow the toString() is recursively calling Nodes somehow</p>
  
  <p>full code:</p>
  
  <p>Node.Java</p>
  
  <pre><code>public class Node {
      Node right;
      Node left;
      String element;
      Node parent;
  
      public Node(){  
  
      }
  
      public Node(String element){
          this.element = element;
  
      }
  
      public String getElement(){
          return element;
      }
  
      public Node getLeft(){
          return left;
      }
  
      public Node getRight(){
          return right;
      }
  
      public void setElement(String str){
          element = str;
      }
  
      public void setRight(Node right){
          this.right = right;
      }
  
      public void setLeft(Node left){
          this.left = left;
      }
  
      public void setParent(Node parent){
          this.parent = parent;
      }
  
      public Node getParent(){
          return parent;
      }
  
      @Override
      public String toString() {
          return "Node [right=" + right + ", left=" + left + ", element=" + element + ", parent=" + parent + "]";
      }
  
  }
  </code></pre>
  
  <p>BinaryTree.Java</p>
  
  <pre><code>import java.util.ArrayList;
  
  public class BinaryTree {
      Node root;
      ArrayList&lt;Node&gt; tree = new ArrayList&lt;&gt;();
  
  
      //create a tree with a root node, and two empty nodes
      //root has no data
      public BinaryTree(Node root){
          tree = new ArrayList&lt;&gt;();
          this.root = root;
          root = new Node();  //parent of root will be set to null
          tree.add(root); // root at position 1 in arrayList
      }
  
  
      public BinaryTree(){
  
      }
  
  
      //assuming one of the children are null and there is a root
      public void insertNode(Node insertNode, Node parent){
          //setthis nodes parent to parent index.
  
  
  
          tree.add(insertNode); // add the node to end of the arraylist.
          insertNode.setParent(parent);  //sets the nodes parent node
          //if parents left is null, 
          if (parent.getLeft()==null){
              parent.setLeft(insertNode);
          }
  
          //else, its right is null
          else{
              parent.setRight(insertNode);
          }
  
      }
  
      public void insertRoot(Node node){
          if (tree.isEmpty()){  //add the root
              root = node;
              tree.add(node);
  
          }
          else{tree.add(node);}// add it, get its location in tree and then get its parent.
          //int index = tree.indexOf(node);// found the index of the nod  
      }
  
  
      public boolean isEmpty(){
          return root==null;
      }
  
      @Override
      public String toString() {
          return "BinaryTree [root=" + root + ", tree=" + tree + ", isEmpty()=" + isEmpty() + "]";
      }
  }
  </code></pre>
  
  <p>TreeTest.java</p>
  
  <pre><code>public class TreeTest {
  
  
  
      public static void main(String[] args){
          //create empty tree &amp; test
          BinaryTree myTree = new BinaryTree();
          System.out.println(myTree.isEmpty());
          System.out.println(myTree.toString());
  
          //create root node &amp; test
          Node myRoot = new Node();  //Node@7852e922
          myTree.insertRoot(myRoot);
          myRoot.setElement("foo");
          System.out.println(myTree.isEmpty());
          System.out.println(myTree.toString());
          System.out.println(myTree.isEmpty());
          System.out.println(myTree.toString());
  
  
          //create a child node and test
          Node secondNode = new Node();  //Node@4e25154f
          myTree.insertNode(secondNode, myRoot);
          System.out.println(myTree.toString());   //code breaks here
          System.out.println(myRoot.getLeft());
  
      }}
  </code></pre>