title: How to create dynamic tree data structure in Java
body: <p>specifically I need to represent the following: </p>
  
  <ol>
  <li>The tree at any node can have an arbitrary number of children </li>
  <li>Each parent node (after the root) is just a String (whose children are also Strings) </li>
  <li>I need to be able to get parent and list out all the children (some sort of list or array of Strings) given an input string representing a given node </li>
  <li>Dynamically populating the tree structure based on reference relationship between parent and child. 
  Example given is I have one member1 sponsor another member2, and member2 sponsor member 3 and so and so for. Already have the table records relationship </li>
  </ol>
  
  <p>Is there an available structure for this ??? </p>
  
  <p><a href="https://i.stack.imgur.com/Ke3wK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ke3wK.png" alt="To build a tree like in the image"></a></p>
  
  <p>My data is from DB or a List, I will loop through the information with the name and the relation to determine if the node is a root, parent or a child. </p>
  
  <p>So during the loop, I found a child, I need a reference to the parent so that I can compare the child relation to the parent before adding the child to its parent. </p>
  
  <p>The closest code I found .</p>
  
  <pre><code>public class TreeNode&lt;T&gt; implements Iterable&lt;TreeNode&lt;T&gt;&gt; {
  
      T data;
      TreeNode&lt;T&gt; parent;
      List&lt;TreeNode&lt;T&gt;&gt; children;
  
      public TreeNode(T data) {
          this.data = data;
          this.children = new LinkedList&lt;TreeNode&lt;T&gt;&gt;();
      }
  
      public TreeNode&lt;T&gt; addChild(T child) {
          TreeNode&lt;T&gt; childNode = new TreeNode&lt;T&gt;(child);
          childNode.parent = this;
          this.children.add(childNode);
          return childNode;
      }
  
      // other features ...
  
  }
  Sample usage:
  
  TreeNode&lt;String&gt; root = new TreeNode&lt;String&gt;("root");
  {
      TreeNode&lt;String&gt; node0 = root.addChild("node0");
      TreeNode&lt;String&gt; node1 = root.addChild("node1");
      TreeNode&lt;String&gt; node2 = root.addChild("node2");
      {
          TreeNode&lt;String&gt; node20 = node2.addChild(null);
          TreeNode&lt;String&gt; node21 = node2.addChild("node21");
          {
              TreeNode&lt;String&gt; node210 = node20.addChild("node210");
          }
      }
  }
  </code></pre>
  
  <p>This is what I have done so far. The parent will get overwritten by the latest entry so hence I am unable to retrieve what I have added previously .</p>
  
  <pre><code>public static TreeNode&lt;String&gt; getSet1() throws IOException {
  
          BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
          String line;
  
          while ((line = reader.readLine()) != null) {
              String[] items = line.split(":");
  
              String name = items[0];
              String parent = items[1];
              String type = items[2];
  
              if (parent.equalsIgnoreCase("-") &amp;&amp; type.equalsIgnoreCase("mainparent")) {
  
                  root = new TreeNode&lt;String&gt;(name);
  
  
              } else if (type.equalsIgnoreCase("ChildParent")  &amp;&amp; parent.equalsIgnoreCase(root.toString())) {
  
                  childParent = root.addChild(name);
  
             } else if (type.equalsIgnoreCase("Child") &amp;&amp; parent.equalsIgnoreCase(childParent.toString())) {
  
                  child = childParent.addChild(name);
              }
  
          }
  
          return root;
      }
  </code></pre>