Recursively populate a tree-graph
<p>I have a data structure where node can have multiple parents.<br>
I have a list of nodes that i want to insert to a tree. 
Node in list contains the data and a sub list of it's parents. </p>

<p>I want to build a tree from this list. </p>

<pre><code>private class Treenode {

        private List&lt;Treenode&gt; children;
        private List&lt;Treenode&gt; parents;

        public List&lt;Treenode&gt; getChildren() {
            return children;
        }

        public List&lt;Treenode&gt; getParents() {
            return parents;
        }

        private Info data;


        public Info getData() {
            return data;
        }

        public void setData(Info data) {
            this.data = data;
        }

        public Treenode() {
            children = new ArrayList&lt;Treenode&gt;();
            parents = new ArrayList&lt;Treenode&gt;();
        }

        public Treenode(Info data) {
            children = new ArrayList&lt;Treenode&gt;();
            parents = new ArrayList&lt;Treenode&gt;();
            this.data = data;
        }

        public boolean addChild(Treenode n) {
            return children.add(n);
        }

        public boolean removeChild(Treenode n) {
            return children.remove(n);
        }
        public boolean addParent(Treenode n) {
            return parents.add(n);
        }

        public boolean removeParent(Treenode n) {
            return parents.remove(n);
        }



    }


private void scanListAndAddToTree(final List list,Treenode parent){

        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Info info = (Info) iter.next();
            String [] parents = info.getParents();
            if(parents==null){ //no parents
                Treenode newNode = new Treenode(info);
                parent.addChild(newNode);
                scanTree(list,newNode);
            } else

            for (int i = 0; i &lt; parents.length; i++) {
                if (parents[i].getID.equals(parent.data.getID())){
                    Treenode newNode = new Treenode(info);
                    parent.addChild(newNode);
                    scanTree(list,newNode);
                }
            }           
</code></pre>

<p>}</p>

<p>But my code is wrong :( </p>

<p>The recursion never stops and re-adds same nodes</p>
