How can I implement a "binary tree structure"?
The result should be:
If the field contains "true" both lists should add this element. If the field contains "false" only the right list should add this element. I think without a recursive function this method is not implementable.
public class Test {
static List<String> fields = new ArrayList<>();
static List<Fieldmatrix> list = new ArrayList<>();
public static void main(String[] args) {
fields.add("1 : true");
fields.add("2 : false");
fields.add("3 : false");
permute(new ArrayList<>(),new ArrayList<>(), 0 , fields);
}
}
Method:
public static List<List<String>> permute(List<String> fieldsRight, List<String> fieldsLeft, int start, List<String> fields){
List<List<String>> combinations = new ArrayList<>();
if (start >= fields.size()) {
combinations.add(fieldsRight);
combinations.add(fieldsLeft);
return combinations;
}
List<String> tmpRight = new ArrayList<>();
List<String> tmpLeft = new ArrayList<>();
if (fields.get(start).contains("false")){
fieldsRight.add(fields.get(start));
tmpRight = new ArrayList<>(fieldsRight);
tmpLeft = new ArrayList<>(fieldsLeft);
combinations.addAll(permute(fieldsRight, fieldsLeft, start+1, fields));
}
else{
fieldsRight.add(fields.get(start));
fieldsLeft.add(fields.get(start));
tmpRight = new ArrayList<>(fieldsRight);
tmpLeft = new ArrayList<>(fieldsLeft);
combinations.addAll(permute(fieldsRight, fieldsLeft, start+1, fields));
}
return combinations;
}
Where is my mistake? How can I solve this problem?
