how to get all combinations from multiple trees?
I have no idea how to get all the possibilities from x sets of data other than using x nested for loops, which i dont want to do, because i dont know the value of x, which makes it very possible for the number of for loops hard coded not equal to the number of loops needed.
say i have:
A:1,2,3,4
B:2,4,65,2
C:1,3,2
D:2,8
开发者_运维百科
and i wanted to get every combination of 1 item from each group (in my code, im using std::vector <myclass>
), how would i do that? can someone please post a general pseudocode i can follow to do this?
if you don't know how many 'groups' you have, I assume you have at least some collection of all of them? i.e., an array/vector of all your vectors?
if so, here's a general idea of what would work
void iterateAllCombinations(array_of_groups, current_group_index,temp_result,callback_function)
{
current_group = array_of_groups[current_group_index]
for each element in current_group
{
temp_result[current_group_index]=element
if current_group_index>0
iterateAllCombinations(array_of_groups,current_group_index-1,temp_result,callback_function)
else
callback_function(temp_result)
}
}
this would be called in some fashion like:
iterateAllCombinations(my_groups,my_groups.length-1,new vector[my_groups.length],&do_something_with_array)
A basic explanation of what's going on: when you call the function, it will start going through each item on the 'top' group (the last one in the array_of_groups). If there's more groups beyond the 'top' one, it will call the same function, passing the current items so far, and telling it to look at the next group down.
This next level down will start going through each item, and behave the same way. This continues until you get to the bottom group, where, instead of calling another level down, it will pass the collection of all results to your callback function, which will use them.
When the bottom level has gone through all items, it will return to the next level up, which is still in its 'for each' loop. the next level up will go to the next item, and call the bottom level, which will start from the beginning. this continues until eventually even the 'top' level has gone through each item.
That's the pseudocode, the specifics change depending on your language. the 'temp result' can be a declared and allocated up-front array, (like I show in the pseudocode), or it can be a stack that gets pushed before each recursive call, and popped after, or it can be an array/vector that you copy and append before each recursive call. You could even have a linked list, with each node just stored in the stack frame for that function call, and you'd just have to pass the pointer of the 'parent' to the next level down.
If you have one specific thing you want to do, you can replace the 'callback_function' with that specific thing. Or, if your language allows it, you can use this as an iterator/generator, and let that ' callback' be implicit
If you really hate recursive code, you can write the same thing as a while loop, but it's much more straightforward to write it as a recursive function.
精彩评论