I have a simple class that computes and ultimately prints all possible combinations of X amount of 6-sided die, using recursive enumeration with a process function. However, while this program works (using found algorithms for the enumeration process), I'm not entirely certain what exactly the recursion is doing that allows the class to sort through ALL possibilities.
The main method is calling the enumeration directly,
enumerate(N, 0, new int[N]);
The enumeration method is quite straightforward,
public static void enumerate(int N, int k, int[] a) {
if (k == N) {
process(a);
return;
}
for (int i = 0; i < 2; i++) {
a[k] = i;
enumerate(N, k + 1, a);
}
}
The process method that is called effectively does nothing at this point, and just prints the array that's passed to it
public static void process(int[] a) {
for (int i : a) {
System.out.print(i);
}
System.out.println();
}
TL;DR - I have an enumerate method that seemingly returns when k==n
- that is, a complete array of possibilities is complete. However, the class returns all possible combinations. What exactly is the recursive function doing that allows this to be possible? Why doesn't the program stop when the enumerate method is returned after forming a complete combination?