title: How to create a recursive algorithm to create a tree in java?
body: <p>I have a database table with records that have <code>StructureGuid</code> and <code>ParentGuid</code>. So most parents have a child.</p>
  
  <p>I want to create a recursive algorithm to build the tree.</p>
  
  <p>The class of the tree is <code>Header.java</code>:</p>
  
  <pre><code>public class Header implements MultiLevelExpIndListAdapter.ExpIndData {
    private List&lt;Header&gt; mChildren;
    private boolean mIsGroup;
    private int mGroupSize;
  
    public String header;
    public String structureGuid;
  
    public Header(String header, String guid){
      this.header = header;
      this.structureGuid = guid;
      mChildren = new ArrayList&lt;Header&gt;();
      setIndentation(0);
    }
  
    @Override public List&lt;? extends MultiLevelExpIndListAdapter.ExpIndData&gt; getChildren() {
      return mChildren;
    }
  
    @Override public boolean isGroup() {
      return mIsGroup;
    }
  
    @Override public void setIsGroup(boolean b) {
      mIsGroup = b;
    }
  
    @Override public void setGroupSize(int i) {
      mGroupSize = i;
    }
  
    public int getGroupSize() {
      return mGroupSize;
    }
  
    public void addChild(Header child){
      mChildren.add(child);
      child.setIndentation(getIndentation() + 1);
    }
  }
  </code></pre>
  
  <p>I use an SQL framework to get the root nodes and then I loop through them. The stop case is when a node (<code>Header</code>) does not have any children.</p>
  
  <p>The code:</p>
  
  <pre><code>//Get the root nodes
  public static List&lt;Header&gt; getHeaders(){
      List&lt;Structure&gt; list = new Select().from(Structure.class)
          .where(Condition.column(Structure$Table.PARENTGUID).eq(""))
          .and(Condition.column(Structure$Table.STATUS).eq(true))
          .and(Condition.column(Structure$Table.REQUIRED).eq(true))
          .orderBy("Sequence ASC").queryList();
  
      ArrayList&lt;Header&gt; headers = new ArrayList&lt;&gt;();
      for (Structure struct: list){
        headers.add(new Header(struct.getDescription(), struct.StructureGUID));
      }
  
      return addChildren(list, headers);
    }
  
  //recursively return child nodes
    public static List&lt;Header&gt; addChildren(List&lt;Structure&gt; structures, List&lt;Header&gt; headers){
      List&lt;Header&gt; newHeader = new ArrayList&lt;&gt;();
      //recurring algorithm - while there are no kids get headers
      if(headers.size() &gt; 0) {
        for (int i = 0; i &lt; headers.size(); i++) {
          //get children of that header - if none return empty arraylist
          //else loop through and addChildren
          List&lt;Structure&gt; list = new Select().from(Structure.class)
              .where(Condition.column(Structure$Table.PARENTGUID).eq(headers.get(i).structureGuid))
              .and(Condition.column(Structure$Table.STATUS).eq(true))
              .and(Condition.column(Structure$Table.REQUIRED).eq(true))
              .orderBy("Sequence ASC").queryList();
  
          List &lt;Header&gt; temp = new ArrayList&lt;Header&gt;();
  
          Log.e("TAG", "size of array: " + list.size());
  
          if (list.size() != 0) {
            for (Structure struct : list) {
              headers.get(i).addChild(new Header(struct.getDescription(), struct.StructureGUID));
              temp.add(headers.get(i));
            }
          }
          newHeader = addChildren(list, temp);
        }
      }
      return newHeader;
    }
  </code></pre>
  
  <p>Right now it gets stuck in a continuous loop on the first child of the first root node.</p>