Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric N-ary Tree (Tree with more than two node per child) Tree Traversal in Java using List for nodes
    primarykey
    data
    text
    <p>I have been trying to implement a tree representation of Autosys job schedules at work. As each job(process) can have one or or more dependent job on them, i decided to go with a n-ary tree implementation so that i can map the flow. I an using java collections for the same.</p> <p>Q1(Solved): The problem for now is that the display functions is getting hung up in a infinite loop. I tried looking for existing threads but could not come across examples for traversals using iterators.</p> <p>Q2:(OPEN)The display function traverse the tree in a Post order manner. I would like to traverse it in a level order manner wherein the nodes get printed on level basis. Root, then all nodes on level one and then all nodes on level 2 and so on. The link for the DFS traversal is posted as another question at the link below. (Hi guys, I have another question for the level order traversal of the same tree, I have posted the question on the link given below. Would be gald if you guys could help. <a href="https://stackoverflow.com/questions/12788641/level-order-traversal-of-a-generic-treen-ary-tree-in-java">Level Order traversal of a generic tree(n-ary tree) in java</a> Let us make this more resourceful for generic trees.)</p> <p>Can we make this a resourceful post for people new with n-ary trees because I found very few elementary examples out there.</p> <p>Here is my code where i have started with a root node with three children. I plan to expand this later.</p> <p>One elementary question. Is using iterator in recursion not advised. I tried definig an Iterator separate in display() but it still didn't work.</p> <p>Current Tree Structure:</p> <blockquote> <pre><code> root(100) / | \ 90 50 70 / \ 20 30 200 300 </code></pre> </blockquote> <p>The output is in post order(not sure if that means DFS).</p> <pre><code>20 30 90 200 300 50 70 100 </code></pre> <p>Code</p> <pre><code>import java.util.*; import java.io.*; import java.util.List; //The node for the n-ary tree public class NaryTree { //The display function traverses the tree void display(NaryTreeNode t) { Iterator&lt;NaryTreeNode&gt; IT = t.nary_list.iterator(); if(!(IT.hasNext())) //If the node does not have any children, enter. { // System.out.println("No more childs of this node"); System.out.print( t.data + " "); return; } while(IT.hasNext()){ display(IT.next()) ; //Recursive Call } System.out.print(t.data + " "); } public static void main(String args[]){ NaryTree t1 = new NaryTree(); NaryTreeNode root = new NaryTreeNode(); root.data = 100; NaryTreeNode lev_11 = new NaryTreeNode(); lev_11.data=90; NaryTreeNode lev_12 = new NaryTreeNode(); lev_12.data=50; NaryTreeNode lev_13 = new NaryTreeNode(); lev_13.data=70; NaryTreeNode lev_21 = new NaryTreeNode(); lev_21.data=20; NaryTreeNode lev_22 = new NaryTreeNode(); lev_22.data=30; NaryTreeNode lev_23 = new NaryTreeNode(); lev_23.data=200; NaryTreeNode lev_24 = new NaryTreeNode(); lev_24.data=300; //Add all the nodes to a list. List&lt;NaryTreeNode&gt; temp2 = new ArrayList&lt;NaryTreeNode&gt;(); //Level two first branch temp2.add(lev_21); temp2.add(lev_22); List&lt;NaryTreeNode&gt; temp3 = new ArrayList&lt;NaryTreeNode&gt;(); //level two second branch temp3.add(lev_23); temp3.add(lev_24); List&lt;NaryTreeNode&gt; temp = new ArrayList&lt;NaryTreeNode&gt;(); //level one temp.add(lev_11); temp.add(lev_12); temp.add(lev_13); lev_11.nary_list.addAll(temp2); lev_12.nary_list.addAll(temp3); //Add Temp to root to form a leaf of the root root.nary_list.addAll(temp); //Call the display function. t1.display(root); </code></pre> <p>} }</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload