Download presentation
Presentation is loading. Please wait.
Published byBrendan Matthews Modified over 9 years ago
1
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall
2
2 Recursive Functions Recursive functions are functions that call themselves Data structures, especially linked implementations of binary trees, sometimes use recursive functions
3
3 Example: Factorial Function The factorial function is often written as a recursive function The factorial of a positive integer is the product of all positive integers less than or equal to the number 5 factorial is written 5! 5! = 5 * 4 * 3 * 2 * 1 = 120 3! = 3 * 2 * 1 = 6 0! is defined to be 1
4
4 Factorial Function 1 int factorial( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial( num – 1 ); 6 }
5
5 Factorial Function 1 int factorial( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial( num – 1 ); 6 } The recursive function call
6
6 What Happens When a Function Calls Itself? When a function calls itself, it is not actually executing itself again Instead, another function is made which is identical Then, that function is called from the recursive function call This will be illustrated in the slides that follow…
7
7 x = factorial( 4 ); A function call that should produce 24 as a result and assign it to x. Recursive Process
8
8 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 4 is passed into num Recursive Process (cont.)
9
9 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 4 replaces each occurrence of num Recursive Process (cont.) 4 44 4
10
10 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } Recursive Process (cont.) 4 44 4
11
11 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } Recursive Process (cont.) 4 44 4
12
12 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } Recursive Process (cont.) 4 44
13
13 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } Recursive Process (cont.) 4 44
14
14 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } Recursive Process (cont.) 4 44 A recursive function call is made – an identical factorial function is made and called.
15
15 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } Recursive Process (cont.) 4 44
16
16 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } Recursive Process (cont.) 4 44
17
17 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } Recursive Process (cont.) 4 44 3 is passed into num
18
18 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } Recursive Process (cont.) 4 44 3 replaces each occurrence of num 33 33
19
19 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } Recursive Process (cont.) 4 44 33 33
20
20 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } Recursive Process (cont.) 4 44 33 33
21
21 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 33 3
22
22 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 33 3
23
23 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 33 3 A recursive function call is made – an identical factorial function is made and called.
24
24 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 33 3
25
25 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 33 3
26
26 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 2 gets passed into num 33 3
27
27 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 2 replaces each occurrence of num 22 22 33 3
28
28 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 22 22 33 3
29
29 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 22 22 33 3
30
30 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } 22 2 33 3
31
31 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } 22 2 33 3
32
32 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } 22 2 A recursive function call is made – an identical factorial function is made and called. 33 3
33
33 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } 22 2 33 3
34
34 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 33 3 22 2
35
35 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 1 is passed into num 33 3 22 2
36
36 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 1 is passed into num 11 11 33 3 22 2
37
37 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 11 11 33 3 22 2
38
38 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 11 11 33 3 22 2
39
39 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 11 11 33 3 22 2
40
40 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 11 1 1 33 3 22 2
41
41 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } Where is 1 returned? 33 3 22 2 11 1 1
42
42 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 33 3 22 2 11 1 1 The 1 replaces the function call that called this function (just as we would expect with any function call)
43
43 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 33 3 22 2 11 1 1
44
44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * 1; } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 33 3 22 2 11 1 1
45
45 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * 1; } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 The last function has finished 33 3 22 2 11 1 1
46
46 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * 1; } x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 33 3 22 2
47
47 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * 1; } x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 The execution of this return statement can now resume 33 3 22 2
48
48 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 2; } x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 The execution of this return statement can now resume 33 3 22
49
49 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 2; } x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 It now returns 2 back to the function call that called this function. 33 3 22
50
50 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 2; } x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 33 3 22
51
51 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 2; } x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 33 3 22
52
52 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 2; } x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 2 ); } Recursive Process (cont.) 4 44 33 3 22
53
53 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 2; } x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * 2; } Recursive Process (cont.) 4 44 33 3 22
54
54 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * 2; } Recursive Process (cont.) 4 44 33 3
55
55 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * 2; } Recursive Process (cont.) 4 44 33 3
56
56 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 6; } Recursive Process (cont.) 4 44 33
57
57 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 6; } Recursive Process (cont.) 4 44 33
58
58 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 6; } Recursive Process (cont.) 4 44 33
59
59 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * 6; } int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 6; } Recursive Process (cont.) 4 44 33
60
60 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * 6; } Recursive Process (cont.) 44 4
61
61 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * 6; } Recursive Process (cont.) 44 4
62
62 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 24; } Recursive Process (cont.) 44
63
63 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 24; } Recursive Process (cont.) 44
64
64 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 24; } Recursive Process (cont.) 44
65
65 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 24; } Recursive Process (cont.) 44 x gets the correct value of 24
66
66 x = 24; int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 24; } Recursive Process (cont.) 44 x gets the correct value of 24
67
67 x = 24; Recursive Process (cont.)
68
68 Base Case 1 int factorial( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial( num – 1 ); 6 } Notice that these lines stopped the recursion – without these lines, the function will call itself over and over again (infinite recursion)
69
69 Base Case (cont.) 1 int factorial( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial( num – 1 ); 6 } These lines are called the base case – the case that stops the recursion
70
70 Recursive Case 1 int factorial( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial( num – 1 ); 6 } This line that produces a recursive function call is called the recursive case. All recursive functions have a base case and a recursive case (and sometimes more than one of each).
71
71 What If? 1 int factorial( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial( num – 1 ); 6 } If one makes a mistake and inputs a negative number into this function: factorial( -2 ); what will happen?
72
72 Infinite Recursion 1 int factorial( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial( num – 1 ); 6 } If one makes a mistake and inputs a negative number into this function: factorial( -2 ); what will happen? Infinite recursion.
73
73 Drivers 1 int factorial( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial( num – 1 ); 6 } In order to prevent this problem, we can change the name of this function to factorial 2…
74
74 Drivers 1 int factorial( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial( num – 1 ); 6 } In order to prevent this problem, we can change the name of this function to factorial 2…
75
75 Drivers (cont.) 1 int factorial2( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial( num – 1 ); 6 } In order to prevent this problem, we can change the name of this function to factorial2…
76
76 Drivers (cont.) 1 int factorial2( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial( num – 1 ); 6 } In order to prevent this problem, we can change the name of this function to factorial2…
77
77 Drivers (cont.) 1 int factorial2( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial2( num – 1 ); 6 } In order to prevent this problem, we can change the name of this function to factorial2…
78
78 Drivers (cont.) 1 int factorial2( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return num * factorial2( num – 1 ); 6 } and then write a factorial function, called a driver, to call this function…
79
79 Drivers (cont.) int factorial( int num ) { if ( num < 0 ) { cout << “The factorial of a negative number is undefined” << endl; return 0; } return factorial2( num ); }
80
80 Guidelines There must be a base case that stops recursion Each recursive call should approach the base case The recursive function call should work for the base case The recursive function call should work for the case next to the base case The recursive function should make logical sense, assuming that the recursive function call inside it does everything it should do
81
81 Example When you know the case next to the base case works, you know factorial( 2 ) works Since 3! = 3 * factorial( 2 ), you know factorial( 3 ) works – makes logical sense Since 4! = 4 * factorial( 3 ) and you know that factorial( 3 ) works, you know that factorial( 4 ) works Etc., etc.
82
82 Recursion on a Linked List Car search( Node *ptr, Car mercedes ) { if ( ptr->info == mercedes ) return ptr->info; return search( ptr->next, mercedes ); } For use when we know Mercedes is in a linked list Initial call: Car auto = search( start, mercedes );
83
83 Recursion on a Linked List (cont.) Car search( Node *ptr, Car mercedes ) { if ( ptr->info == mercedes ) return ptr->info; return search( ptr->next, mercedes ); } Overloaded operator in Car struct
84
84 Recursion on a Linked List (cont.) Car search( Node *ptr, Car mercedes ) { if ( ptr->info == mercedes ) return ptr->info; return search( ptr->next, mercedes ); } Car search( Node *ptr, Car mercedes ) { if ( ptr->info == mercedes ) return ptr->info; return search( ptr->next, mercedes ); } Advances pointer in a recursive function call
85
85 Recursion on a Linked List (cont.) bool search( Node *ptr, Car & auto, Car mercedes ) { if ( ptr == NULL ) return false; if ( ptr->info == mercedes ) { auto = ptr->info; return true; } return search( ptr->next, auto, mercedes ); } For use when we are not sure there is a Mercedes in the linked list.
86
86 Recursion on a Linked List (cont.) bool search( Node *ptr, Car & auto, Car mercedes ) { if ( ptr == NULL ) return false; if ( ptr->info == mercedes ) { auto = ptr->info; return true; } return search( ptr->next, auto, mercedes ); } Returns true if in list; returns false otherwise.
87
87 Recursion on a Linked List (cont.) bool search( Node *ptr, Car & auto, Car mercedes ) { if ( ptr == NULL ) return false; if ( ptr->info == mercedes ) { auto = ptr->info; return true; } return search( ptr->next, auto, mercedes ); } If true is returned, auto will be assigned Mercedes (passed by reference on each recursive call)
88
88 Recursion on a Linked List (cont.) bool search( Node *ptr, Car & auto, Car mercedes ) { if ( ptr == NULL ) return false; if ( ptr->info == mercedes ) { auto = ptr->info; return true; } return search( ptr->next, auto, mercedes ); } Two base cases
89
89 Recursion on a Linked List (cont.) bool search( Node *ptr, Car & auto, Car mercedes ) { if ( ptr == NULL ) return false; if ( ptr->info == mercedes ) { auto = ptr->info; return true; } return search( ptr->next, auto, mercedes ); } Don’t forget the return ( it is a common mistake)…
90
90 Recursion on a Linked List (cont.) bool search( Node *ptr, Car & auto, Car mercedes ) { if ( ptr == NULL ) return false; if ( ptr->info == mercedes ) { auto = ptr->info; return true; } return search( ptr->next, auto, mercedes ); } It passes the true/false value (from base cases) back through the succession of recursive function calls.
91
91 Recursion on a Linked List (cont.) 1 void discount( Node *ptr ) 2 { 3 if ( ptr != NULL ) { 4ptr->info.price -= 0.1 * ( ptr->info.price ); 5discount( ptr->next ); 6} 7 } Discounts all auto prices in a linked list by 10%
92
92 Recursion on a Linked List (cont.) 1 void discount( Node *ptr ) 2 { 3 if ( ptr != NULL ) { 4ptr->info.price -= 0.1 * ( ptr->info.price ); 5discount( ptr->next ); 6} 7 } Recursive call – no return necessary (void return type)
93
93 Recursion on a Linked List (cont.) 1 void discount( Node *ptr ) 2 { 3 if ( ptr != NULL ) { 4ptr->info.price -= 0.1 * ( ptr->info.price ); 5discount( ptr->next ); 6} 7 } Where is the base case?
94
94 Recursion on Linked Lists (cont.) 1 void discount( Node *ptr ) 2 { 3 if ( ptr != NULL ) { 4ptr->info.price -= 0.1 * ( ptr->info.price ); 5discount( ptr->next ); 6} 7 } The base case exists, it just does not need to be written. When ptr == NULL, it is the base case. The only thing that needs to be done for the base case is to return.
95
95 Time Complexities for Recursion Recursion is an alternative to a loop Recursion is never necessary – anything that can be done with recursion can be done with a loop Just as we would estimate how many times a loop iterates, we must estimate how many times a recursive function is called – this gives the time complexity
96
96 Time Complexities for Recursion (cont.) 1 void discount( Node *ptr ) 2 { 3 if ( ptr != NULL ) { 4ptr->info.price -= 0.1 * ( ptr->info.price ); 5discount( ptr->next ); 6} 7 } This function is called recursively, once for each of the n elements in a linked list, plus one more time for when ptr == NULL, giving us a total number of n + 1 calls – its time complexity, therefore, is ( n )
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.