title: Parse Nodes in a Java Tree Structure in Vector of Strings
body: <p>I have a data structure that looks like this </p>
  
  <pre><code>private String name;
  private ArrayList&lt;Node&gt; children;
  private String parent="";
  
  public Node(String name) {
  setName(name);
  children = new ArrayList&lt;Node&gt;();
  }
  </code></pre>
  
  <p>Elsewhere in my program, I have a Node called "root" that contains an entire tree data structure.</p>
  
  <p>Conceptually it looks like this </p>
  
  <pre><code>                                       root
                                        /     \
                                       /       \
                                    node1     node2
                                     /           \
                                    /             \
                                  node2          node3
                                  / 
                                 /
                               node3
  </code></pre>
  
  <p>As you can see nodes can have the same name. That's intended. I want to create a string for each node that contains its own name, plus it's lineage and store them in a Vector.</p>
  
  <p>so node 3 on the left hand side would be <code>"root|node1|node2|node3"</code> the node3 on the rhs would be <code>"root|node2|node3"</code> node1 would be <code>"root|node1"</code>etc.</p>
  
  <p>I have a way to iterate through the node structure to print each node, but I'm finding it difficult to set every parent, as in, I can't figure out a way to do it. Any help would be fantastic as everything I've tried so far has failed. One important note is that the tree may not necessarily  be a Binary tree, I'm just using it for an example.</p>
  
  <p>Here's the code I use for printing every node of the tree. Hopefully it will be easy to tweak.</p>
  
  <pre><code>public void print() {
          LinkedList&lt;Node&gt; open = new LinkedList&lt;Node&gt;();
          LinkedList&lt;Node&gt; closed = new LinkedList&lt;Node&gt;();
  
          open.add(this);
  
          while(!open.isEmpty()) {
              Node currentNode = open.removeFirst();
              System.out.println(currentNode.getName());
  
              ArrayList&lt;Node&gt; children = currentNode.getChildren();
              closed.add(currentNode);
  
              for(int i = 0; i &lt; children.size(); i++) {
                  Node current = children.get(i);
                  open.addLast(current);
              }
          }
      }
  </code></pre>
  
  <p>Thanks guys.</p>