To generalize this question I am borrowing material from a Zelenski CS class handout. And, it is relevant to my specific question since I took the class from a different instructor several years ago and learned this approach to C++. The handout is here. My understanding of C++ is low since I use it occasionally. Basically, the few times I have needed to write a program I return to the class material, found something similar and started from there.
In this example (page 4) Julie is looking for a word using a recursive algorithm in a string function. To reduce the number of recursive calls she added a decision point bool containsWord().
string FindWord(string soFar, string rest, Lexicon &lex)
{
if (rest.empty()) {
return (lex.containsWord(soFar)? soFar : "");
} else {
for (int i = 0; i < rest.length(); i++) {
string remain = rest.substr(0, i) + rest.substr(i+1);
string found = FindWord(soFar + rest[i], remain, lex);
if (!found.empty()) return found;
}
}
return ""; // empty string indicates failure
}
To add flexibility to how this algorithm is used, can this be implemented as a void type?
void FindWord(string soFar, string rest, Lexicon &lex, Set::StructT &words)
{
if (rest.empty()) {
if (lex.containsWord(soFar)) //this is a bool
updateSet(soFar, words); //add soFar to referenced Set struct tree
} else {
for (int i = 0; i < rest.length(); i++) {
string remain = rest.substr(0, i) + rest.substr(i+1);
return FindWord(soFar + rest[i], remain, lex, words); //<-this is where I am confused conceptually
}
}
return; // indicates failure
}
And, how about without the returns
void FindWord(string soFar, string rest, Lexicon &lex, Set::StructT &words)
{
if (rest.empty()) {
if (lex.containsWord(soFar))
updateSet(soFar, words); //add soFar to Set memory tree
} else {
for (int i = 0; i < rest.length(); i++) {
string remain = rest.substr(0, i) + rest.substr(i+1);
FindWord(soFar + rest[i], remain, lex, words); //<-this is where I am confused conceptually
}
}
}
The first code fragment will try all permutations of
rest, appended to the initial value ofsoFar(probably an empty string?). It will stop on the first word found that is inlex. That word will be returned immediately as it is found, and the search will be cut short at that point. If none were inlex, empty string will be returned eventually, when all theforloops have ran their course to the end.The second fragment will only try one word: the concatenation of initial
soFarandreststrings. If that concatenated string is inlex, it will callupdateSetwith it. Then it will return, indicating failure. No further search will be performed, because thereturnfrom inside theforloop is unconditional.So these two functions are completely different. To make the second code behave like the first, you need it to return something else to indicate a success, and only return from within the
forloop whenFindWordcall return value indicates a success. Obviously,voidcan not be used to signalfailureandsuccess. At the very least, you need to returnboolvalue for that.And without the returns your third code will perform an exhaustive search. Every possible permutation of initial string value of
restwill be tried for, to find in the lexicon.You can visualize what’s going on like this:
Each time the base case is reached (
restis empty) we haven+1FindWordcall frames on the stack, fornletters in the initialreststring.Each time we hit the bottom, we’ve picked all the letters from
rest. The check is performed to see whether it’s inlex, and control returns back one level up.So if there are no returns, each
forloop will run to its end. If the return is unconditional, only one permutation will be tried – the trivial one. But if the return is conditional, the whole thing will stop only on first success.