What am I missing? I have my base case and it appears that the 'left side' runs with no problem, but I get the error when the right side gets executed. I'm fairly new to recursion and I'm sure the answer is pretty obvious, but I can't seem to find it.
Basically, I have a list that I'm trying to see how many subsets are in that list that, when added, equal the constant D. I'm using an ArrayList with true and false values to create a decision tree, then using that list to find the sums using "test", a global ArrayList.
public void recSubSetSum(ArrayList<Boolean> subSetList){
if(calcSumOfList(subSetList) == D)
subsetsFound++;
else if(calcSumOfList(subSetList) < D){
for (int i = 0; i < test.size(); i++) {
ArrayList leftList = new ArrayList<>();
copyList(leftList, subSetList);
leftList.add(true);
if(calcSumOfList(leftList) < D)
recSubSetSum(leftList);
ArrayList rightList = new ArrayList<>();
copyList(rightList, subSetList);
rightList.add(false);
if(calcSumOfList(rightList) < D)
recSubSetSum(rightList); //this is where the error occurs
}//for
}//else
else{
subsetsNotFound++;
}
}
public int calcSumOfList(ArrayList<Boolean> boolList){
int sum = 0;
int j =0; //I used j because it was giving me a outOfBoundsException when I uses i
for (int i = 0; i < boolList.size(); i++) {
if(boolList.get(i) == true){
sum+= test.get(j);
j++;
}
}//for
return sum;
}//calc sum of list
Thanks in advance!
This is because the
if
statement before the error line is incorrect:The condition is already satisfied by the outer if statement:
This is because you are copying the contents of
subSetList
intorightList
and thecalcSumOfList
does not add to the value of the list if an entry isfalse
, makingrightList
the same value as the originalsubSetList
. So the method recurses every time, causing a StackOverflow.