title: Storing a binary tree in a file
body: <p>So I am trying store a binary tree in a file. I am doing this to store it in the file.</p>
  
  <pre><code>public String[] getTree() {
      ArrayList&lt;String&gt; list = new ArrayList();
      String[] str;
      preOrder(root, list);
      str = list.toArray(new String[list.size()]);
      return str;
  }
  
  private void preOrder(Node current, ArrayList list) {
      if (current == null) {
          list.add("&amp;");
          return;
      }
      list.add(current.getValue());
      preOrder(current.getLeft(), list);
      preOrder(current.getRight(), list);
  }
  </code></pre>
  
  <p>Basically every null node I represent as an &amp;, and each node is separated by a line break. With this array I send it to file. Now when the program starts up again it reads this file and puts it back in the array. However I am having issues wrapping my head around the process for this and nothing that I read seems to help. This is the current algorithm I am trying, however it does not work for large trees.</p>
  
  <pre><code>private void makeBinaryTree(String[] in) {
      root = new Node(in[0]);
      Node current = root;
      boolean left = true;
      for(int i = 1; i &lt; in.length; i++){
          if(!in[i].equals("&amp;")){
              if(left){
                  Node toAdd = new Node(in[i]);
                  toAdd.setParent(current);
                  current.setLeft(toAdd);
                  current = toAdd;
              }else{
                  Node toAdd = new Node(in[i]);
                  toAdd.setParent(current);
                  current.setRight(toAdd);
                  current = toAdd;
                  left = true;
              }
  
  /*            }else if(left &amp;&amp; in[i].equals("&amp;")){
                    left = false;
            */}else if(!left &amp;&amp; in[i].equals("&amp;")){
              left = false;
              current = current.getParent();
              current = current.getParent();
          }else if(i+1 &gt;= in.length){
          }else if(in[i].equals("&amp;") &amp;&amp; in[i+1].equals("&amp;")){
              current = current.getParent();
              left = (!left);
              i++;
          }
      }
  
  }
  </code></pre>
  
  <p>Thanks for all the help.</p>