I have the following n-ary tree. The value in the nodes are of the form names {count}. The count value of any node is the number of paths that include the node.
I need to print all paths(from root to leaf) in the tree until the count of that node becomes zero. Once the count of the node is zero, then path only up to its predecessor is printed(provided their count is greater than 0)
The 6 paths for the above graph would be
3-5-1-2-4-6
3-5-1-2
3-5-1-7
3-5-2
3-1-4
3-7
[EDIT] Tried the following. Called the function toString(indent,node) using a loop that runs root.count number of times. But don't know how to stop after a path(3-5-1-2-4-6) has no more childs. Instead it prints 3-5-1-2-4-6-7-2-1-4-7.
public String toString(String indent, MyTree node) {
String output="";
while(node.count>0){
//output = indent + node.toString() + "\n";
output = indent + node.name;
node.count--;
//if(node.count==0) break;
String childsOutput = "";
for (MyTree child : node.childs) {
childsOutput += toString(indent + " ", child);
}
return output + childsOutput;
}
return output;
}
Thanks in advance.