ch11

29
Chapter 11 Recursion To learn about the method of recursion To understand the relationship between recursion and iteration To analyze problems that are much easier to solve by recursion than by iteration To learn to “think recursively” To be able to use recursive helper functions To understand when the use of recursion affects the efficiency of an algorithm C HAPTER G OALS T he method of recursion is a powerful technique to break up complex computational problems into simpler ones. The term “recursion” refers to the fact that the same computation recurs, or occurs repeatedly, as the problem is solved. Recursion is often the most natural way of thinking about a problem, and there are some computations that are very difficult to perform without recursion. This chapter shows you both simple and complex examples of recursion and teaches you how to “think recursively”.

Upload: tanika-dewi-sofianti

Post on 26-Nov-2014

203 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ch11

Chapter

11

Recursion

To learn about the method of recursion

To understand the relationship between recursion and iteration

To analyze problems that are much easier to solve by recursion than by iteration

To learn to “think recursively”

To be able to use recursive helper functions

To understand when the use of recursion affects the efficiency of an algorithm

CHAPTER GOALS

T

he method of

recursion is a powerful technique to break up complex

computational problems into simpler ones. The term “recursion” refers to the fact

that the same computation recurs, or occurs repeatedly, as the problem is solved.

Recursion is often the most natural way of thinking about a problem, and there are

some computations that are very difficult to perform without recursion. This

chapter shows you both simple and complex examples of recursion and teaches you

how to “think recursively”.

sc_ch11.fm Page 1 Sunday, September 7, 2008 8:34 PM

Page 2: ch11

2

CHAPTER

11

Recursion

C

HAPTER

C

ONTENTS

C++ for Everyone

, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

We begin this chapter with a very simple example that demonstrates the power ofthinking recursively. In this example, we will look at triangle shapes such as this:

[][][][][][]

We will compute the area of a triangle of width

n

, assuming that each

[]

square hasarea 1. This value is sometimes called the

nth triangle number

. For example, as youcan tell from looking at the above triangle, the third triangle number is 6.

You may know that there is a very simple formula to compute these numbers,but you should pretend for now that you don’t know about it. The ultimate pur-pose of this section is not to compute triangle numbers, but to learn about the con-cept of recursion in a simple situation.

Here is the outline of the class that we will develop:

class Triangle{public: Triangle(int w); int get_area() const;private: int width; };

Triangle::Triangle(int w){ width = w;}

If the width of the triangle is 1, then the triangle consists of a single square, and itsarea is 1. Take care of this case first.

int Triangle::get_area(){ if (width == 1) return 1; ...}

11.1 Tr iangle Numbers

11.1 Triangle Numbers 2

C

OMMON

E

RROR

11.1: Infinite Recursion

5

11.2 Permutations 6

C

OMMON

E

RROR

11.2: Tracing Through

Recursive Functions

9

11.3 Thinking Recursively 11

11.4 Recursive Helper Functions 14

11.5 Mutual Recursion 15

11.6 The Efficiency of Recursion 20

sc_ch11.fm Page 2 Sunday, September 7, 2008 8:34 PM

Page 3: ch11

11.1

Triangle Numbers

3

C++ for Everyone

, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

To deal with the general case, consider this picture.

[][][][][][][][][][]

Suppose you knew the area of the smaller, colored triangle. Then you could easilycompute the area of the larger triangle as

smaller_area + width

How can you get the smaller area? Make a smaller triangle and ask it!

Triangle smaller_triangle(width - 1);int smaller_area = smaller_triangle.get_area();

Now we can complete the

get_area

function:

int Triangle::get_area() const{ if (width == 1) return 1; Triangle smaller_triangle(width - 1); int smaller_area = smaller_triangle.get_area(); return smaller_area + width;}

Here is an illustration of what happens when we compute the area of a triangle ofwidth 4.

• The

get_area

function makes a smaller triangle of width 3.

• It calls

get_area

on that triangle.

• That function makes a smaller triangle of width 2.

• It calls

get_area

on that triangle.

• That function makes a smaller triangle of width 1.

• It calls

get_area

on that triangle.

• That function returns 1.

• The function returns

smaller_area + width

= 1 + 2 = 3.

• The function returns

smaller_area + width

= 3 + 3 = 6.

• The function returns

smaller_area + width

= 6 + 4 = 10.

This solution has one remarkable aspect. To solve the area problemfor a triangle of a given width, we use the fact that we can solve thesame problem for a lesser width. This is called a

recursive

solution.The call pattern of a recursive function looks complicated, and the

key to the successful design of a recursive function is

not to thinkabout it

. Instead, look at the

get_area

function one more time andnotice how utterly reasonable it is. If the width is 1, then of course

the area is 1. The next part is just as reasonable. Compute the area of the smaller tri-angle

and don’t think about why that works

. Then the area of the larger triangle isclearly the sum of the smaller area and the width.

A recursive computation solves a problem by using the solution to the same problem with simpler inputs.

sc_ch11.fm Page 3 Sunday, September 7, 2008 8:34 PM

Page 4: ch11

4

CHAPTER

11

Recursion

C++ for Everyone

, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

There are two key requirements to make sure that the recursion is successful:

• Every recursive call must simplify the computation in some way.• There must be special cases to handle the simplest computations directly.

The

get_area

function calls itself again with smaller and smallerwidth values. Eventually the width must reach 1, and there is a spe-cial case for computing the area of a triangle with width 1. Thus, the

get_area

function always succeeds.Actually, you have to be careful. What happens when you call the

area of a triangle with width –1? It computes the area of a trianglewith width –2, which computes the area of a triangle with width –3, and so on. Toavoid this, the

get_area

function should return 0 if the width is

0.Recursion is not really necessary to compute the triangle numbers. The area of a

triangle equals the sum

1 + 2 + 3 + ... + width

Of course, we can program a simple loop:

double area = 0;for (int i = 1; i <= width; i++) area = area + i;

Many simple recursions can be computed as loops. However, loop equivalents formore complex recursions—such as the one in our next example—can be complex.

Actually, in this case, you don’t even need a loop to compute the answer. Thesum of the first

n

integers can be computed as

Thus, the area equals

width * (width + 1) / 2

Therefore, neither recursion nor a loop are required to solve this problem. Therecursive solution is intended as a “warm-up” for the next section.

ch11/triangle.cpp

For a recursion to terminate, there must be special cases for the simplest inputs.

1 2 1 2+ + + = × +( )� n n n

1 #include <iostream>23 using namespace std;45 /**6 A class that describes triangle shapes like this:7 []8 [][]9 [][][]10 . . .11 */12 class Triangle13 {

sc_ch11.fm Page 4 Sunday, September 7, 2008 8:34 PM

Page 5: ch11

11.1

Triangle Numbers

5

C++ for Everyone

, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Infinite Recursion

A common programming error is an infinite recursion: a function calling itself over and overwith no end in sight. The computer needs some amount of memory for bookkeeping foreach call. After some number of calls, all memory that is available for this purpose isexhausted. Your program shuts down and reports a “stack fault”.

Infinite recursion happens either because the parameter values don’t get simpler orbecause a terminating case is missing. For example, suppose the

get_area

function computesthe area of a triangle with width 0. If it weren’t for the special test, the function would haveconstructed triangles with width –1, –2, –3, and so on.

14 public:15 Triangle(int w);16 int get_area() const;17 private:18 int width; 19 };2021 /**22 Constructs a triangle with a given width.23 @param w the width of the triangle base24 */25 Triangle::Triangle(int w)26 {27 width = w;28 }2930 /**31 Computes the area of the triangle shape.32 @return the area33 */34 int Triangle::get_area() const35 {36 if (width <= 0) return 0;37 if (width == 1) return 1;38 Triangle smaller_triangle(width - 1);39 int smaller_area = smaller_triangle.get_area();40 return smaller_area + width;41 }4243 int main()44 {45 Triangle t(4);46 cout << "Area: " << t.get_area() << "\n";47 return 0;48 }

COMMON ERROR 11.1

sc_ch11.fm Page 5 Sunday, September 7, 2008 8:34 PM

Page 6: ch11

6 CHAPTER 11 • Recursion

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

In this section, we consider a more complex example of recursion that would be dif-ficult to program with a simple loop. Our task is to generate all permutations of astring. A permutation is simply a rearrangement of the letters. For example, thestring "eat" has six permutations (including the original string itself):

"eat""eta""aet""ate""tea""tae"

We will develop a functionvector<string> generate_permutations(string word)

that generates all permutations of a word.Here is how you would use the function. The following code displays all permu-

tations of the string "eat":vector<string> v = generate_permutations("eat");for (int i = 0; i < v.size(); i++){ cout << v[i] << "\n";}

Now you need a way to generate the permutations recursively. Consider the string"eat" and simplify the problem. First, generate all permutations that start with theletter 'e', then those that start with 'a', and finally those that start with 't'. Howdo you generate the permutations that start with 'e'? You need to know the permu-tations of the substring "at". But that’s the same problem—to generate all permuta-tions—with a simpler input, namely the shorter string "at". Using recursiongenerates the permutations of the substring "at". You will get the strings

"at""ta"

For each result of the simpler problem, add the letter 'e' in front. Now you have allpermutations of "eat" that start with 'e', namely

"eat""eta"

Next, turn your attention to the permutations of "eat" that start with 'a'. You mustcreate the permutations of the remaining letters, "et", namely:

"et""te"

Add the letter 'a' to the front of the strings and obtain"aet""ate"

11.2 Permutat ions

sc_ch11.fm Page 6 Sunday, September 7, 2008 8:34 PM

Page 7: ch11

11.2 • Permutations 7

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Generate the permutations that start with 't' in the same way.That’s the idea. To carry it out, you must implement a loop that iterates through

the character positions of the word. Each loop iteration creates a shorter word thatomits the current position:

vector<string> generate_permutations(string word){ vector<string> result; ... for (int i = 0; i < word.length(); i++) { string shorter_word = word.substr(0, i) + word.substr(i + 1); ... } return result;}

The next step is to compute the permutations of the shorter word. vector<string> shorter_permutations = generate_permutations(shorter_word);

For each of the shorter permutations, add the omitted letter:for (int j = 0; j < shorter_permutations.size(); j++){ string longer_word = word[i] + shorter_permutations[j]; result.push_back(longer_word);}

The permutation generation algorithm is recursive—it uses the fact that we can gen-erate the permutations of shorter words. When does the recursion stop? You mustbuild in a stopping point, a special case to handle words of length 1. A word oflength 1 has a single permutation, namely itself. Here is the added code to handle aword of length 1.

vector<string> generate_permutations(string word){ vector<string> result; if (word.length() <= 1) { result.push_back(word); return result; } ...}

The complete program is at the end of this section.Could you generate the permutations without recursion? There is

no obvious way of writing a loop that iterates through all permuta-tions. Exercise P11.12 shows that there is an iterative solution, but itis far more difficult to understand than the recursive algorithm.

For generating permutations, it is much easier to use recursion than iteration.

sc_ch11.fm Page 7 Sunday, September 7, 2008 8:34 PM

Page 8: ch11

8 CHAPTER 11 • Recursion

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

ch11/permute.cpp

Program Run

1 #include <iostream>2 #include <string>3 #include <vector>45 using namespace std;67 /**8 Generates all permutations of the characters in a string.9 @param word a string10 @return a vector that is filled with all permutations 11 of the word12 */13 vector<string> generate_permutations(string word)14 {15 vector<string> result;16 if (word.length() <= 1) 17 {18 result.push_back(word);19 return result;20 }2122 for (int i = 0; i < word.length(); i++)23 {24 string shorter_word = word.substr(0, i)25 + word.substr(i + 1);26 vector<string> shorter_permutations27 = generate_permutations(shorter_word);28 for (int j = 0; j < shorter_permutations.size(); j++)29 {30 string longer_word = word[i] + shorter_permutations[j];31 result.push_back(longer_word);32 } 33 }34 return result;35 }3637 int main()38 {39 cout << "Enter a string: ";40 string input;41 getline(cin, input); 42 vector<string> v = generate_permutations(input);43 for (int i = 0; i < v.size(); i++)44 cout << v[i] << "\n";45 return 0;46 }

Enter a string: armarmamrram

sc_ch11.fm Page 8 Sunday, September 7, 2008 8:34 PM

Page 9: ch11

11.2 • Permutations 9

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Tracing Through Recursive Functions

Debugging a recursive function can be somewhat challenging. When you set a breakpoint ina recursive function, the program stops as soon as that program line is encountered in anycall to the recursive function. Suppose you want to debug the recursive get_area function ofthe Triangle class. Run until the beginning of the get_area function (Figure 1). Inspect thewidth instance variable. It is 4.

Figure 1 Debugging a Recursive Function

rmamarmra

COMMON ERROR 11.2

sc_ch11.fm Page 9 Sunday, September 7, 2008 8:34 PM

Page 10: ch11

10 CHAPTER 11 • Recursion

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Remove the breakpoint and now run until the statement return smaller_area + width;

When you inspect width again, its value is 2! That makes no sense. There was no instructionthat changed the value of width! Is that a bug with the debugger?

No. The program stopped in the first recursive call to get_area that reached the returnstatement. If you are confused, look at the call stack (Figure 2). You will see that three calls toget_area are pending.

You can debug recursive functions with the debugger. You just need to be particularlycareful, and watch the call stack to understand which nested call you currently are in.

Figure 2 Three Calls to get_area Are Pending

sc_ch11.fm Page 10 Sunday, September 7, 2008 8:34 PM

Page 11: ch11

11.3 • Thinking Recursively 11

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

To solve a problem recursively requires a different mindset than to solve it by pro-gramming loops. In fact, it helps if you are, or pretend to be, a bit lazy and let othersdo most of the work for you. If you need to solve a complex problem, pretend that“someone else” will do most of the heavy lifting and solve the problem for all sim-pler inputs. Then you only need to figure out how you can turn the solutions withsimpler inputs into a solution for the whole problem.

This section gives you a step-by-step guide to the method of recursion. To illus-trate the steps, use the following problem to test whether a sentence is a palin-drome—a string that is equal to itself when you reverse all characters. Typicalexamples of palindromes are

• rotor• A man, a plan, a canal—Panama!• Go hang a salami, I’m a lasagna hog

and, of course, the oldest palindrome of all:

• Madam, I’m Adam

Our goal is to implement the predicate functionbool is_palindrome(string s)

For simplicity, assume for now that the string has only lowercase letters and nopunctuation marks or spaces. Exercise P11.13 asks you to generalize the function toarbitrary strings.

Step 1 Consider various ways for simplifying inputs.

In your mind, fix a particular input or set of inputs for the problem that you wantto solve. Think how you can simplify the inputs in such a way that the same prob-lem can be applied to the simpler input.

When you consider simpler inputs, you may want to remove just alittle bit from the original input—maybe remove one or two charac-ters from a string, or remove a small portion of a geometric shape.But sometimes it is more useful to cut the input in half and then seewhat it means to solve the problem for both halves.

In the palindrome test problem, the input is the string that we needto test. How can you simplify the input? Here are several possibilities:

• Remove the first character.• Remove the last character.• Remove both the first and the last character.• Remove a character from the middle.• Cut the string into two halves.

These simpler inputs are all potential inputs for the palindrome test.

11.3 Think ing Recurs ive ly

The key step in finding a recursive solution is reducing the input to a simpler input for the same problem.

sc_ch11.fm Page 11 Sunday, September 7, 2008 8:34 PM

Page 12: ch11

12 CHAPTER 11 • Recursion

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Step 2 Combine solutions with simpler inputs to a solution of the originalproblem.

In your mind, consider the solutions of your problem for the simpler inputs thatyou have discovered in Step 1. Don’t worry how those solutions are obtained. Sim-ply have faith that the solutions are readily available. Just say to yourself: These aresimpler inputs, so someone else will solve the problem for me.

Now think how you can turn the solution for the simpler inputs into a solutionfor the input that you are currently thinking about. Maybe you need to add a smallquantity, related to the quantity that you lopped off to arrive at the simpler input.Maybe you cut the original input in two halves and have solutions for both halves.Then you may need to add both solutions to arrive at a solution for the whole.

Consider the methods for simplifying the inputs for the palindrome test. Cuttingthe string in half doesn’t seem a good idea. If you cut

"rotor"

in half, you get two strings:"rot"

and"or"

Neither of them is a palindrome. Cutting the input in half and testing whether thehalves are palindromes seems a dead end.

The most promising simplification is to remove the first and last characters. Removing the r at the front and back of "rotor" yields"oto"

Suppose you can verify that the shorter string is a palindrome. Then of course theoriginal string is a palindrome—we put the same letter in the front and the back.That’s extremely promising. A word is a palindrome if

• The first and last letters match, and• The word obtained by removing the first and last letters is a palindrome.

Again, don’t worry how the test works for the shorter string. It just works.

Step 3 Find solutions to the simplest inputs.

A recursive computation keeps simplifying its inputs. Eventually it arrives at verysimple inputs. To make sure that the recursion comes to a stop, you must deal withthe simplest inputs separately. Come up with special solutions for them. That isusually very easy.

However, sometimes you get into philosophical questions dealing with degener-ate inputs: empty strings, shapes with no area, and so on. Then you may want toinvestigate a slightly larger input that gets reduced to such a trivial input and seewhat value you should attach to the degenerate inputs so that the simpler value,when used according to the rules you discovered in Step 2, yields the correctanswer.

sc_ch11.fm Page 12 Sunday, September 7, 2008 8:34 PM

Page 13: ch11

11.3 • Thinking Recursively 13

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Look at the simplest strings for the palindrome test:

• Strings with two characters• Strings with a single character• The empty string

You don’t have to come up with a special solution for strings with two characters.Step 2 still applies to those strings—either or both of the characters are removed.But you do need to worry about strings of length 0 and 1. In those cases, Step 2can’t apply. There aren’t two characters to remove.

A string with a single character, such as "I", is a palindrome. The empty string is a palindrome—it’s the same string when you read it back-

wards. If you find that too artificial, consider a string "oo". According to the rulediscovered in Step 2, this string is a palindrome if the first and last character of thatstring match and the remainder—that is, the empty string—is also a palindrome.Therefore, it makes sense to consider the empty string a palindrome.

Thus, all strings of length 0 or 1 are palindromes.

Step 4 Implement the solution by combining the simple cases and the reductionstep.

Now you are ready to implement the solution. Make separate cases for the simpleinputs that you considered in Step 3. If the input isn’t one of the simplest cases, thenimplement the logic you discovered in Step 2.

The following program shows the complete is_palindrome function.

ch11/palindrome.cpp

1 #include <iostream>2 #include <string>3 #include <vector>45 using namespace std;67 /**8 Tests whether a string is a palindrome. A palindrome9 is equal to its reverse, for example “rotor” or “racecar”.10 @param s a string11 @return true if s is a palindrome12 */13 bool is_palindrome(string s)14 {15 // Separate case for shortest strings16 if (s.length() <= 1) return true;1718 // Get first and last character, converted to lowercase19 char first = s[0];20 char last = s[s.length() - 1];2122 if (first == last)23 {

sc_ch11.fm Page 13 Sunday, September 7, 2008 8:34 PM

Page 14: ch11

14 CHAPTER 11 • Recursion

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Program Run

You have now seen several recursive algorithms, all of which workon the same principle. When given a complex input, they first solvethe problem with a simpler input. Then they turn the simpler resultinto the result for the more complex input. This process is quite intu-itive as long as you think about the solution on that level only. How-ever, behind the scenes, the function that computes the simpler inputcalls yet another function that works on even simpler input, which

calls yet another, and so on, until one function’s input is so simple that it can com-pute the results without further help. It is interesting to think about that process,but it can also be confusing. What’s important is that you can focus on the one levelthat matters—putting a solution together from the slightly simpler problem, ignor-ing the fact that it also uses recursion to get its results.

Sometimes it is easier to find a recursive solution if you change theoriginal problem slightly. Then the original problem can be solved bycalling a recursive helper function.

Here is a typical example. Consider the palindrome test of the pre-ceding section. It is a bit inefficient to construct new string objectsin every step. Now consider the following change in the problem.

Rather than testing whether the entire sentence is a palindrome, check whether asubstring is a palindrome:

24 string shorter = s.substr(1, s.length() - 2);25 return is_palindrome(shorter);26 }27 else28 return false;29 }3031 int main()32 {33 cout << "Enter a string: ";34 string input;35 getline(cin, input);36 cout << input << " is ";37 if (!is_palindrome(input)) cout << "not "; 38 cout << "a palindrome\n";39 return 0;40 }

Enter a string: aibohphobiaaibohphobia is a palindrome.

When designing a recursive solution, do not worry about multiple nested calls. Simply focus on reducing a problem to a slightly simpler one.

11.4 Recurs ive Helper Funct ions

Sometimes it is easier to find a recursive solution if you make a slight change to the original problem.

sc_ch11.fm Page 14 Sunday, September 7, 2008 8:34 PM

Page 15: ch11

11.5 • Mutual Recursion 15

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

/* Tests whether a substring of a string is a palindrome. @param s the string to test @param start the index of the first character of the substring @param end the index of the last character of the substring @return true if the substring is a palindrome*/bool substring_is_palindrome(string s, int start, int end)

This function turns out to be even easier to implement than the original test. In therecursive calls, simply adjust the start and end parameters to skip over matchingletter pairs. There is no need to construct new string objects to represent theshorter strings.

bool substring_is_palindrome(string s, int start, int end){ // Separate case for substrings of length 0 and 1 if (start >= end) return true;

if (s[start] == s[end]) // Test substring that doesn’t contain the first and last letters return substring_is_palindrome(s, start + 1, end - 1); else return false;}

You should supply a function to solve the whole problem—the user of your func-tion shouldn’t have to know about the trick with the substring positions. Simplycall the helper function with positions that test the entire string:

bool is_palindrome(string s){ return substring_is_palindrome(s, 0, s.length() - 1);}

Note that the is_palindrome function is not recursive. It just calls a recursive helperfunction.

Use the technique of recursive helper functions whenever it is easier to solve arecursive problem that is slightly different from the original problem.

In the preceding examples, a function called itself to solve a simplerproblem. Sometimes, a set of cooperating functions calls each otherin a recursive fashion. In this section, we will explore a typical situa-tion of such a mutual recursion.

We will develop a program that can compute the values of arith-metic expressions such as

3 + 4 * 5(3 + 4) * 51 - (2 - (3 - (4 - 5)))

11.5 Mutua l Recurs ion

In a mutual recursion, a set of cooperating functions calls each other repeatedly.

sc_ch11.fm Page 15 Sunday, September 7, 2008 8:34 PM

Page 16: ch11

16 CHAPTER 11 • Recursion

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Computing such an expression is complicated by the fact that * and / bind morestrongly than + and -, and that parentheses can be used to group subexpressions.

Figure 3 shows a set of syntax diagrams that describes the syntax of these expres-sions. An expression is either a term, or a sum or difference of terms. A term iseither a factor, or a product or quotient of factors. Finally, a factor is either a num-ber or an expression enclosed in parentheses.

Figure 4 shows how the expressions 3 + 4 * 5 and (3 + 4) * 5 are derived fromthe syntax diagram.

Figure 3Syntax Diagrams for Evaluating an Expression

termexpression

+

factorterm

*

/

expression

numbernumber

factor

( )

Figure 4 Syntax Trees for Two Expressions

Expression

Term Term

Factor

Number

3 + * *

Factor

Number

4

Factor

Number

5 5

Expression

Factor

Term

Factor

Number

3( +

Expression

Term

Term

Factor

Number

4 )

Factor

Number

sc_ch11.fm Page 16 Sunday, September 7, 2008 8:34 PM

Page 17: ch11

11.5 • Mutual Recursion 17

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Why do the syntax diagrams help us compute the value of the tree? If you look atthe syntax trees, you will see that they accurately represent which operationsshould be carried out first. In the first tree, 4 and 5 should be multiplied, and thenthe result should be added to 3. In the second tree, 3 and 4 should be added, and theresult should be multiplied by 5.

To compute the value of an expression, we implement three functions:expression_value, term_value, and factor_value. The expression_value functionfirst calls term_value to get the value of the first term of the expression. Then itchecks whether the next input character is one of + or -. If so, it calls term_valueagain and adds or subtracts it.

int expression_value(){ int result = term_value(); bool more = true; while (more) { char op = cin.peek(); if (op == '+' || op == '-') { cin.get(); int value = term_value(); if (op == '+') result = result + value; else result = result - value; } else more = false; } return result;}

The term_value function calls factor_value in the same way, multiplying or dividingthe factor values.

Finally, the factor_value function checks whether the next input character is a'(' or a digit. In the latter case, the value is simply the value of the number. How-ever, if the function sees a parenthesis, the factor_value function makes a recursivecall to expression_value. Thus, the three functions are mutually recursive.

int factor_value(){ int result = 0; char c = cin.peek(); if (c == '(') { cin.get(); result = expression_value(); cin.get(); // read ")" } else // Assemble number value from digits { while (isdigit(c)) { result = 10 * result + c - '0'; cin.get(); c = cin.peek();

sc_ch11.fm Page 17 Sunday, September 7, 2008 8:34 PM

Page 18: ch11

18 CHAPTER 11 • Recursion

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

} } return result;}

As always with a recursive solution, you need to ensure that the recursion termi-nates. In this situation, that is easy to see. If expression_value calls itself, the secondcall works on a shorter subexpression than the original expression. At each recur-sive call, at least some of the characters of the input are consumed, so eventually therecursion must come to an end.

ch11/eval.cpp

1 #include <iostream>2 #include <cctype>34 using namespace std;56 int term_value();7 int factor_value();89 /**10 Evaluates the next expression found in cin.11 @return the value of the expression12 */13 int expression_value()14 {15 int result = term_value();16 bool more = true;17 while (more)18 {19 char op = cin.peek();20 if (op == '+' || op == '-')21 {22 cin.get();23 int value = term_value();24 if (op == '+') result = result + value;25 else result = result - value;26 }27 else more = false;28 }29 return result;30 }3132 /**33 Evaluates the next term found in cin.34 @return the value of the term.35 */36 int term_value()37 {38 int result = factor_value();39 bool more = true;

sc_ch11.fm Page 18 Sunday, September 7, 2008 8:34 PM

Page 19: ch11

11.5 • Mutual Recursion 19

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Program Run

40 while (more)41 {42 char op = cin.peek();43 if (op == '*' || op == '/')44 {45 cin.get();46 int value = factor_value();47 if (op == '*') result = result * value;48 else result = result / value;49 }50 else more = false;51 }52 return result;53 }5455 /**56 Evaluates the next factor found in cin.57 @return the value of the factor.58 */59 int factor_value()60 {61 int result = 0;62 char c = cin.peek();63 if (c == '(')64 {65 cin.get();66 result = expression_value();67 cin.get(); // Read ")"68 }69 else // Assemble number value from digits70 {71 while (isdigit(c))72 {73 result = 10 * result + c - '0';74 cin.get();75 c = cin.peek();76 } 77 }78 return result;79 }8081 int main()82 {83 cout << "Enter an expression: ";84 cout << expression_value() << "\n";85 return 0;86 }

Enter an expression: 1+12*12*121729

sc_ch11.fm Page 19 Sunday, September 7, 2008 8:34 PM

Page 20: ch11

20 CHAPTER 11 • Recursion

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

As you have seen in this chapter, recursion can be a powerful tool toimplement complex algorithms. On the other hand, recursion can leadto algorithms that perform poorly. In this section, we will analyze thequestion of when recursion is beneficial and when it is inefficient.

The Fibonacci sequence is a sequence of numbers defined by theequation

That is, each value of the sequence is the sum of the two preceding values. The firstten terms of the sequence are

1, 1, 2, 3, 5, 8, 13, 21, 34, 55

It is easy to extend this sequence indefinitely. Just keep appending the sum of thelast two values of the sequence. For example, the next entry is 34 + 55 = 89.

We would like to write a function that computes fn for any value of n. Supposewe translate the definition directly into a recursive function:

ch11/fibtest.cpp

11.6 The Ef f ic iency of Recurs ion

Occasionally, a recursive solution runs much slower than its iterative counterpart. However, in most cases, the recursive solution runs at about the same speed.

f

f

f f fn n n

1

2

1 2

1

1

=

=

= +− −

1 #include <iostream>23 using namespace std;45 /**6 Computes a Fibonacci number.7 @param n an integer8 @return the nth Fibonacci number9 */10 int fib(int n)11 { 12 if (n <= 2) return 1;13 else return fib(n - 1) + fib(n - 2);14 }1516 int main()17 { 18 cout << "Enter n: ";19 int n;20 cin >> n;21 int f = fib(n);22 cout << "fib(" << n << ") = " << f << "\n";23 return 0;24 }

sc_ch11.fm Page 20 Sunday, September 7, 2008 8:34 PM

Page 21: ch11

11.6 • The Efficiency of Recursion 21

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Program Run

That is certainly simple, and the function will work correctly. But watch the outputclosely as you run the test program. For small input values, the program is quite fast.Even for moderately large values, though, the program pauses an amazingly longtime between outputs. Try out some numbers between 30 and 50 to see this effect.

That makes no sense. Armed with pencil, paper, and a pocket calculator youcould calculate these numbers pretty quickly, so it shouldn’t take the computer long.

To determine the problem, insert trace messages into the function:

ch11/fibtrace.cpp

Program Run

Enter n: 6fib(6) = 8

1 #include <iostream>23 using namespace std;45 /**6 Computes a Fibonacci number.7 @param n an integer8 @return the nth Fibonacci number9 */10 int fib(int n)11 { 12 cout << "Entering fib: n = " << n << "\n";13 int f;14 if (n <= 2) f = 1;15 else f = fib(n - 1) + fib(n - 2);16 cout << "Exiting fib: n = " << n17 << " return value = " << f << "\n";18 return f;19 }2021 int main()22 { 23 cout << "Enter n: ";24 int n;25 cin >> n;26 int f = fib(n);27 cout << "fib(" << n << ") = " << f << "\n";28 return 0;29 }

Enter n: 6

Entering fib: n = 6Entering fib: n = 5Entering fib: n = 4Entering fib: n = 3Entering fib: n = 2

sc_ch11.fm Page 21 Sunday, September 7, 2008 8:34 PM

Page 22: ch11

22 CHAPTER 11 • Recursion

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Figure 5 shows the call tree.Now it is becoming apparent why the function takes so long. It is computing the

same values over and over. For example, the computation of fib(6) calls fib(4)twice and fib(3) three times. That is very different from the computation youwould do with pencil and paper. There you would just write down the values as

Figure 5 Call Pattern of the Recursive fib Function

Exiting fib: n = 2 return value = 1Entering fib: n = 1Exiting fib: n = 1 return value = 1Exiting fib: n = 3 return value = 2Entering fib: n = 2Exiting fib: n = 2 return value = 1Exiting fib: n = 4 return value = 3Entering fib: n = 3Entering fib: n = 2Exiting fib: n = 2 return value = 1Entering fib: n = 1Exiting fib: n = 1 return value = 1Exiting fib: n = 3 return value = 2Exiting fib: n = 5 return value = 5Entering fib: n = 4Entering fib: n = 3Entering fib: n = 2Exiting fib: n = 2 return value = 1Entering fib: n = 1Exiting fib: n = 1 return value = 1Exiting fib: n = 3 return value = 2Entering fib: n = 2Exiting fib: n = 2 return value = 1Exiting fib: n = 4 return value = 3Exiting fib: n = 6 return value = 8

fib(6) = 8

fib(6)

fib(5) fib(4)

fib(4) fib(3) fib(3) fib(2)

fib(3) fib(2) fib(2) fib(1) fib(2) fib(1)

fib(2) fib(1)

sc_ch11.fm Page 22 Sunday, September 7, 2008 8:34 PM

Page 23: ch11

11.6 • The Efficiency of Recursion 23

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

they were computed and add up the last two to get the next one until you reachedthe desired entry; no sequence value would ever be computed twice.

If you imitate the pencil-and-paper process, then you get the following program.

ch11/fibloop.cpp

This function runs much faster than the recursive version. In this example of the fib function, the recursive solution was easy to program

because it exactly followed the mathematical definition, but it ran far more slowlythan the iterative solution, because it computed many intermediate results multipletimes.

Can you always speed up a recursive solution by changing it into a loop? Fre-quently, the iterative and recursive solution have essentially the same performance.For example, here is an iterative solution for the palindrome test.

public bool is_palindrome(string s){ int start = 0; int end = text.length() - 1;

1 #include <iostream>23 using namespace std;45 /**6 Computes a Fibonacci number.7 @param n an integer8 @return the nth Fibonacci number9 */10 int fib(int n)11 { 12 if (n <= 2) return 1;13 int fold = 1;14 int fold2 = 1;15 int fnew;16 for (int i = 3; i <= n; i++)17 { 18 fnew = fold + fold2;19 fold2 = fold;20 fold = fnew;21 }22 return fnew;23 }2425 int main()26 { 27 cout << "Enter n: ";28 int n;29 cin >> n;30 int f = fib(n);31 cout << "fib(" << n << ") = " << f << "\n";32 return 0;33 }

sc_ch11.fm Page 23 Sunday, September 7, 2008 8:34 PM

Page 24: ch11

24 CHAPTER 11 • Recursion

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

while (start < end) { if (s[start] != s[end]) return false; start++; end--; } return true;}

This solution keeps two index variables: start and end. The first index starts at thebeginning of the string and is advanced whenever a letter has been matched or anon-letter has been ignored. The second index starts at the end of the string andmoves toward the beginning. When the two index variables meet, then the iterationstops.

Both the iteration and the recursion run at about the same speed. If a palindromehas n characters, the iteration executes the loop times. Similarly, the recursivesolution calls itself times, because two characters are removed in each step.

In such a situation, the iterative solution tends to be a bit faster, because eachrecursive function call takes a certain amount of processor time. In principle, it ispossible for a smart compiler to avoid recursive function calls if they follow simplepatterns, but most compilers don’t do that. From that point of view, an iterativesolution is preferable.

Often, recursive solutions are easier to understand and implementcorrectly than their iterative counterparts. There is a certain eleganceand economy of thought to recursive solutions that makes themmore appealing. As the computer scientist (and creator of the Ghost-Script interpreter for the PostScript graphics description language) L.Peter Deutsch put it: “To iterate is human, to recurse divine.”

1. A recursive computation solves a problem by using the solution to the sameproblem with simpler inputs.

2. For a recursion to terminate, there must be special cases for the simplest inputs.

3. For generating permutations, it is much easier to use recursion than iteration.

4. The key step in finding a recursive solution is reducing the input to a simplerinput for the same problem.

5. When designing a recursive solution, do not worry about multiple nested calls.Simply focus on reducing a problem to a slightly simpler one.

6. Sometimes it is easier to find a recursive solution if you make a slight change tothe original problem.

7. In a mutual recursion, a set of cooperating functions calls each other repeatedly.

n 2n 2

In many cases, a recursive solution is easier to understand and implement correctly than an iterative solution.

CHAPTER SUMMARY

sc_ch11.fm Page 24 Sunday, September 7, 2008 8:34 PM

Page 25: ch11

Review Exercises 25

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

8. Occasionally, a recursive solution runs much slower than its iterative counter-part. However, in most cases, the recursive solution runs at about the same speed.

9. In many cases, a recursive solution is easier to understand and implement cor-rectly than an iterative solution.

Exercise R11.1. Define the terms a. recursionb. iterationc. infinite recursiond. mutual recursion

Exercise R11.2. The factorial function counts the number of permutations of n objects. It is recursively defined by the equations

and

Following this definition, determine the values for 1! and 2!. Explain why these are the correct counts for permutations of 1 and 2 objects.

Exercise R11.3. Outline, but do not implement, a recursive solution for finding the smallest value in an array.

Exercise R11.4. Outline, but do not implement, a recursive solution for sorting an array of numbers. Hint: First find the smallest value in the array.

Exercise R11.5. Outline, but do not implement, a recursive solution for generating all subsets of the set {1, 2, ... , n}.

Exercise R11.6. Exercise P11.12 shows an iterative way of generating all permuta-tions of the sequence (0, 1, ... , n – 1). Explain why the algorithm produces the right result.

Exercise R11.7. Write a recursive definition of xn, where x ≥ 0, similar to the recursive definition of the Fibonacci numbers. Hint: How do you compute xn from xn–1? How does the recursion terminate?

Exercise R11.8. Write a recursive definition of , similar to the recursive definition of the Fibonacci numbers.

Exercise R11.9. Find out how often the recursive version of fib calls itself. Keep a global variable fib_count and increment it once in every call of fib. What is the rela-tionship between fib(n) and fib_count?

REVIEW EXERCISES

n n n! != −( ) ×1

0 1! =

n n! = × × ×1 2 …

sc_ch11.fm Page 25 Sunday, September 7, 2008 8:34 PM

Page 26: ch11

26 CHAPTER 11 • Recursion

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Exercise R11.10. How many moves are required to move n disks in the “Towers of Hanoi” problem of Exercise P11.14? Hint: As explained in the exercise,

moves(1) = 1 moves(n) = 2 · moves(n – 1) + 1

Exercise P11.1. If a string has n letters, then the number of permutations is given by the factorial function:

For example, and there are six permutations of the three-character string "eat". Implement a recursive factorial function, using the definitions

and

Exercise P11.2. Write a recursive function void reverse() that reverses a sentence. For example:

Sentence greeting = new Sentence("Hello!");greeting.reverse();cout << greeting.get_text() << "\n";

prints the string "!olleH". Implement a recursive solution by removing the first character, reversing a sentence consisting of the remaining text, and combining the two.

Exercise P11.3. Redo Exercise P11.2 with a recursive helper function that reverses a substring of the message text.

Exercise P11.4. Implement the reverse function of Exercise P11.2 as an iteration.

Exercise P11.5. Use recursion to implement a function bool find(string s, string t) that tests whether a string t is contained in a string s:

bool b = s.find("Mississippi!", "sip"); // Returns true

Hint: If the text starts with the string you want to match, then you are done. If not, consider the sentence that you obtain by removing the first character.

Exercise P11.6. Use recursion to implement a function int index_of(string s, string t) that returns the starting position of the first substring of the string s that matches t. Return –1 if t is not a substring of s. For example,

int n = s.index_of("Mississippi!", "sip"); // Returns 6

Hint: This is a bit trickier than Exercise P11.5, because you need to keep track of how far the match is from the beginning of the sentence. Make that value a parame-ter of a helper function.

PROGRAMMING EXERCISES

n n! = × × × … ×1 2 3

3 1 2 3 6! = × × =

n n n! != −( ) ×1

0 1! =

sc_ch11.fm Page 26 Sunday, September 7, 2008 8:34 PM

Page 27: ch11

Programming Exercises 27

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

Exercise P11.7. Using recursion, find the largest element in a vector of integer values. int maximum(vector<int> values)

Hint: Find the largest element in the subset containing all but the last element. Then compare that maximum to the value of the last element.

Exercise P11.8. Using recursion, compute the sum of all values in an array.

Exercise P11.9. Using recursion, compute the area of a polygon. Cut off a triangle and use the fact that a triangle with corners , , has area

.

Exercise P11.10. Implement a functionvector<string> generate_substrings(string s)

that generates all substrings of a string. For example, the substrings of the string "rum" are the seven strings

"r", "ru", "rum", "u", "um", "m", ""

Hint: First enumerate all substrings that start with the first character. There are n of them if the string has length n. Then enumerate the substrings of the string that you obtain by removing the first character.

Exercise P11.11. Implement a functionvector<string> generate_subsets(string s)

that generates all subsets of characters of a string. For example, the subsets of char-acters of the string "rum" are the eight strings

"rum", "ru", "rm", "r", "um", "u", "m", ""

Note that the subsets don’t have to be substrings—for example, "rm" isn’t a sub-string of "rum".

Exercise P11.12. The following program generates all permutations of the numbers 0, 1, 2, ... , n – 1, without using recursion.

using namespace std;

void swap(int& x, int& y){

x y1 1,( ) x y2 2,( ) x y3 3,( )x y x y x y y x y x y x1 2 2 3 3 1 1 2 2 3 3 1 2+ + − − −( )

sc_ch11.fm Page 27 Sunday, September 7, 2008 8:34 PM

Page 28: ch11

28 CHAPTER 11 • Recursion

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

int temp = x; x = y; y = temp;}

void reverse(vector<int>& a, int i, int j){ while (i < j) { swap(a[i], a[j]); i++; j--; }}

bool next_permutation(vector<int>& a){ for (int i = a.size() - 1; i > 0; i--) { if (a[i - 1] < a[i]) { int j = a.size() - 1; while (a[i - 1] > a[j]) j--; swap(a[i - 1], a[j]); reverse(a, i, a.size() - 1); return true; } } return false;}

void print(const vector<int>& a){ for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } cout << "\n";}

int main(){ const int n = 4; vector<int> a(n); for (int i = 0; i < a.size(); i++) { a[i] = i; } print(a); while (next_permutation(a)) { print(a); } return 0;}

sc_ch11.fm Page 28 Sunday, September 7, 2008 8:34 PM

Page 29: ch11

Programming Exercises 29

C++ for Everyone, Cay Horstmann, Copyright © 2009 John Wiley & Sons, Inc. All Rights Reserved.

The algorithm uses the fact that the set to be permuted consists of distinct numbers. Thus, you cannot use the same algorithm to compute the permutations of the char-acters in a string. You can, however, use this technique to get all permutations of the character positions and then compute a string whose ith character is s[a[i]]. Use this approach to reimplement the generate_permutations function without recursion.

Exercise P11.13. Refine the is_palindrome function to work with arbitrary strings, by ignoring non-letter characters and the distinction between upper- and lowercase letters. For example, if the input string is

"Madam, I’m Adam!"

then you’d first strip off the last character because it isn’t a letter, and recursively check whether the shorter string

"Madam, I’m Adam"

is a palindrome.

Exercise P11.14. Towers of Hanoi. This is a well-known puzzle. A stack of disks of decreasing size is to be transported from the left-most peg to the right-most peg. The middle peg can be used as a temporary storage. (See Figure 6.) One disk can be moved at one time, from any peg to any other peg. You can place smaller disks only on top of larger ones, not the other way around.

Write a program that prints the moves necessary to solve the puzzle for n disks. (Ask the user for n at the beginning of the program.) Print moves in the form

Move disk from peg 1 to peg 3

Hint: Write a helper functionvoid hanoi(int from, int to, int n)

that moves the top n disks from the peg from to the peg to. Then figure out how you can achieve that by first moving the pile of the top n - 1 disks to the third peg, mov-ing the nth disk to the destination, and then moving the pile from the third peg to the destination peg, this time using the original peg as the temporary storage. Extra credit if you write the program to actually draw the moves using the graphics library or “ASCII art”!

Figure 6 Towers of Hanoi

sc_ch11.fm Page 29 Sunday, September 7, 2008 8:34 PM