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.
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.
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. Level Order traversal of a generic tree(n-ary tree) in java Let us make this more resourceful for generic trees.)
Can we make this a resourceful post for people new with n-ary trees because I found very few elementary examples out there.
Here is my code where i have started with a root node with three children. I plan to expand this later.
One elementary question. Is using iterator in recursion not advised. I tried definig an Iterator separate in display() but it still didn't work.
Current Tree Structure:
root(100) / | \ 90 50 70 / \ 20 30 200 300
The output is in post order(not sure if that means DFS).
20 30 90 200 300 50 70 100
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<NaryTreeNode> 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<NaryTreeNode> temp2 = new ArrayList<NaryTreeNode>(); //Level two first branch
temp2.add(lev_21);
temp2.add(lev_22);
List<NaryTreeNode> temp3 = new ArrayList<NaryTreeNode>(); //level two second branch
temp3.add(lev_23);
temp3.add(lev_24);
List<NaryTreeNode> temp = new ArrayList<NaryTreeNode>(); //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);
} }