DFSTree { public List<String> generateCombinations() { ArrayList<String> results = Lists.newArrayList(); Collection<List<DFSTree>> permutations = Collections2.permutations(nodes); for (List<DFSTree> permutation : permutations) { results.add(label + "," + Joiner.on(',').join(permutation)); } List<String> childrenLabels = Lists.newArrayList(); for (DFSTree node : nodes) { if(!node.nodes.isEmpty()) { Collection<List<DFSTree>> childrenPermutation = Collections2.permutations(node.nodes); for (List<DFSTree> child : childrenPermutation) { childrenLabels.add(Joiner.on(',').join(child)); } } } if(childrenLabels.isEmpty()) { return results; } List<String> listOfString = Lists.newArrayList(); List<List<String>> lists = Lists.cartesianProduct(results, childrenLabels); Joiner j =Joiner.on(","); for (List<String> list : lists) { listOfString.add(j.join(list)); } return listOfString; } DFSTree(int label); void edge(DFSTree child); List<String> generateCombinations(); @Override String toString();  }
@Test public void a_node_with_one_child_generates_one_plan(){ DFSTree tree = DFSTreeBuilder.aTreeBuilder().withLabel(0).withChild(DFSTreeBuilder.aTreeBuilder().withLabel(1)).build(); assertThat(tree.generateCombinations()).isEqualTo(Lists.newArrayList("0,1")) ; }
@Test public void a_node_with_two_children_generates_two_plans(){ DFSTree tree = DFSTreeBuilder.aTreeBuilder().withLabel(0).withChild(DFSTreeBuilder.aTreeBuilder().withLabel(1)). withChild(DFSTreeBuilder.aTreeBuilder().withLabel(2)).build(); assertThat(tree.generateCombinations()).isEqualTo(Lists.newArrayList("0,1,2","0,2,1")); }
@Test public void a_node_with_two_levels_generates_two_plans(){ DFSTree tree = DFSTreeBuilder.aTreeBuilder().withLabel(0) .withChild(DFSTreeBuilder.aTreeBuilder().withLabel(1) .withChild(DFSTreeBuilder.aTreeBuilder().withLabel(2)) .withChild(DFSTreeBuilder.aTreeBuilder().withLabel(3))).build(); assertThat(tree.generateCombinations()).isEqualTo(Lists.newArrayList("0,1,2,3","0,1,3,2")); }