title: Construct N-ary tree from ArrayList in java
body: <p>I am trying to construct N-ary tree from an ArrayList.The N-ary tree is represented as binary with child and sibling pointers.
  Here is the class Node, each node element carries data that should be print out with preorder traversal.</p>
  
  <pre><code>public class Node {
  
  
      public String data;
      public int ID,parentID;
  
      public Node child,sibling;  
  
  
  
      public Node(){}
  
      public Node(String data,int ID,int parentID)
      {
          this.data = data;
          this.ID = ID;
          this.parentID = parentID;
  
      }   
  
  }
  </code></pre>
  
  <p>This is the class tree.</p>
  
  <pre><code>public class NTree
  {
      public Node root;
  
      public NTree(){}    
      public void addTreeNode(Node parent, Node newChild)
      {
  
          if(parent.child == null)
           {
              parent.child = newChild;            
              return;
           }
  
          Node temp = parent.child;
  
          while(temp.sibling != null)
          {
              temp = temp.sibling;
          }
          temp.sibling = newChild;        
  
      }
  
  
      public Node find_parentNode(ArrayList&lt;Node&gt; nodes ,int parentID)
      {
          for(int i= 0;i&lt;nodes.size();i++)
          {
              if(nodes.get(i).parentID == parentID)
                  return nodes.get(i);
          }
  
          return null;
      }
  
      public  void preorder(Node root)
      {
  
             if (root == null) return;
  
               System.out.println(root.data);
               preorder(root.child);          
               preorder(root.sibling);
  
      }
  </code></pre>
  
  <p>In the main program, I have set the root as one node that has parentID = 0 and array list of Nodes is fine.In order to add Tree node, I must have which is the parent node and which is the new node.When the preorder function is called it crashes at the: preorder(root.child); which I think there is some problem with the creation of the tree. Any ideas?</p>
  
  <pre><code>for(int i = 0; i &lt; list_nodes.size(); i++)
  {                                                                   
        Node parent = tree.find_parentNode(list_nodes, list_nodes.get(i).parentID);                                       
        tree.addTreeNode(parent, list_nodes.get(i));              
  }
  tree.preorder(tree.root);
  
  
  
  . ComException in thread "main" java.lang.StackOverflowError
      at java.io.FileOutputStream.write(Unknown Source)
      at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
      at java.io.BufferedOutputStream.flush(Unknown Source)
      at java.io.PrintStream.write(Unknown Source)
      at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
      at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
      at sun.nio.cs.StreamEncoder.flushBuffer(Unknown Source)
      at java.io.OutputStreamWriter.flushBuffer(Unknown Source)
      at java.io.PrintStream.write(Unknown Source)
      at java.io.PrintStream.print(Unknown Source)
      at java.io.PrintStream.println(Unknown Source)
      at solution.NTree.preorder(NTree.java:77)
      at solution.NTree.preorder(NTree.java:78)
  </code></pre>