Beruflich Dokumente
Kultur Dokumente
Objectives
In this chapter you will: Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover what is a recursive algorithm Learn about recursive functions Explore how to use recursive functions to implement recursive algorithms
C++ Programming: From Problem Analysis to Program Design, Second Edition 2
SOME EXAMPLES . . .
5
And notice that the recursive call Summation(n - 1) gets us closer to the base case of Summation(1)
n 4
Returns 4 + Summation(3) = 4 + 6 = 10
Returns 3 + Summation(2) = 3 + 3 = 6
Call 2:
Summation(3)
n 3
Returns 2 + Summation(1)
Call 3:
Summation(2)
n 2
=2+1= 3
n==1
Returns 1
Call 4:
Summation(1)
n 1
10
And notice that the recursive call Factorial(n - 1) gets us closer to the base case of Factorial(0).
11
Recursive Solution
int Factorial ( int number ) // Pre: number is assigned and number >= 0. { if ( number == 0) // base case return 1 ; // general case else return number * Factorial ( number - 1 ) ; }
12
// CAN REWRITE AS . . .
13 13
{ if ( n > 0 ) // general case { cout << << endl ; PrintStars ( n - 1 ) ; } // base case is empty else-clause }
14
14
n 3 Call 2:
PrintStars(2)
* is printed n 2 Call 3:
PrintStars(1)
* is printed
* is printed
n 1 Call 4:
PrintStars(0) Do nothing
n 0
15
74
data[0]
36
data[1]
87
95
data[2] data[3]
95
87
36 74
16
17
PrintRev(data, 0, 2) Trace
Call 1:
first 0 PrintRev(data, 0, 2) last 2
data[2] printed
Call 2:
PrintRev(data, 0, 1) data[1] printed
first 0 last 1
Call 3:
PrintRev(data, 0, 0) data[0] printed
first 0 last 0
Call 4:
NOTE: data address 6000 is also passed PrintRev(data, 0, -1) Do nothing
first 0 last -1
19
Recursive Functions
You can have: One base case and one recursive case An empty (does nothing) base case Multiple base cases Multiple recursive cases But you MUST always have a base case (even if it does nothing) to avoid infinite recursion
C++ Programming: From Problem Analysis to Program Design, Second Edition 20
21
22
struct NodeType
struct NodeType { char NodeType* } NodeType* head ;
component ; link ;
23
RevPrint(head);
head A B C D E
FIRST, print out this section of list, backwards THEN, print this element
24
Recall that . . .
recursion occurs when a function calls itself (directly or indirectly) recursion can be used in place of iteration (looping) some functions can be written more easily using recursion
25
Recursion or Iteration?
CLARITY
EFFICIENCY
26
27
28
29
// Another recursive function int Func ( /* in */ int a, /* in */ int b ) // Pre: Assigned(a) && Assigned(b) // Post: Function value == ?? { int result; if ( b == 0 ) result = 0; else if ( b > 0 ) // first general case // base case
result = a + Func ( a , b - 1 ) ; // instruction 50 else // second general case result = Func ( - a , - b ) ; // instruction 70
return result; }
30
30
? ? 2 5 100
? ? 1 5 50 ? 5+Func(5,1) = ? 2 5 100
FCTVAL 5 result 5+Func(5,0) = 5+ 0 b 1 a 5 Return Address 50 FCTVAL result b a Return Address ? 5+Func(5,1) = ? 2 5 100
36
Towers of Hanoi
The Towers of Hanoi problem is an example of a problem that is easy to solve with recursion, difficult to solve with iteration
37
Tower of Hanoi
The rules for moving disks are as follows:
Only one disk can be moved at a time The removed disk must be placed on one of the needles A larger disk cannot be placed on top of a smaller disk
39
40
Programming Example
Use recursion to convert a non-negative integer in decimal format (base 10) into the equivalent binary number (base 2) Define some terms:
Let x be an integer The remainder of x after division by 2 is the rightmost bit of x The rightmost bit of 33 is 1 because 33 % 2 is 1 The rightmost bit of 28 is 0 because 28 % 2 is 0
C++ Programming: From Problem Analysis to Program Design, Second Edition 42
44
45
Summary
The process of solving a problem by reducing it to smaller versions of itself is called recursion A function is called recursive if it calls itself Every recursive definition has one or more base cases The solution to the problem in a base case is obtained directly
47
Summary
The general case must eventually be reduced to a base case The base case stops the recursion
48