C++ Plus Data Structures: Nell Dale
C++ Plus Data Structures: Nell Dale
C++ Plus Data Structures: Nell Dale
Nell Dale
Chapter 7
Programming with Recursion
1
Recursive Function Call
A recursive call is a function call in
which the called function is the same as
the one making the call.
SOME EXAMPLES . . .
4
Writing a recursive function to find
n factorial
DISCUSSION
if ( number == 0 )
return 1;
5
Writing a recursive function to find
Factorial(n)
Now for the general case . . .
or, n * Factorial(n - 1)
7
Three-Question Method of verifying
recursive functions
8
Another example where
recursion comes naturally
From mathematics, we know that
20 = 1 and 25 = 2 * 24
In general,
x0 = 1 and xn = x * xn-1
for integer x, and integer n > 0.
9
// Recursive definition of power function
struct ListType
{
int length ; // number of elements in the list
};
ListType list ;
11
Recursive function to determine if
value is in list
PROTOTYPE
bool ValueInList( ListType list , int value , int startIndex ) ;
74 36 ... 95 75 29 47 ...
list[0] [1] [startIndex] [length -1]
index
Already searched of Needs to be searched
current
element
to
examine 12
bool ValueInList ( ListType list , int value , int startIndex )
14
struct ListType
struct NodeType
{
int info ;
NodeType* next ;
}
class SortedType {
public :
private :
NodeType* listData ;
};
15
RevPrint(listData);
listData
A B C D E
THEN, print
this element
16
Base Case and General Case
A base case may be a solution in terms of a “smaller” list.
Certainly for a list with 0 elements, there is no more
processing to do.
}
// Base case : if the list is empty, do nothing
} 18
Function BinarySearch( )
19
found = BinarySearch(info, 25, 0, 14 );
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
info
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28
16 18 20 22 24 26 28
24 26 28
24
NOTE: denotes element examined
20
// Recursive definition
template<class ItemType>
bool BinarySearch ( ItemType info[ ] , ItemType item ,
int fromLoc , int toLoc )
// Pre: info [ fromLoc . . toLoc ] sorted in ascending order
// Post: Function value = ( item in info [ fromLoc . . toLoc] )
{ int mid ;
if ( fromLoc > toLoc ) // base case -- not found
return false ;
else {
mid = ( fromLoc + toLoc ) / 2 ;
if ( info [ mid ] == item ) // base case-- found at mid
return true ;
else if ( item < info [ mid ] ) // search lower half
return BinarySearch ( info, item, fromLoc, mid-1 ) ;
else // search upper half
return BinarySearch( info, item, mid + 1, toLoc ) ;
}
21
}
When a function is called...
22
Stack Activation Frames
The activation record stores the return address
for this function call, and also the parameters,
local variables, and the function’s return value,
if non-void.
FCTVAL ?
call in Func(5,1) code
result ? at instruction 50
b 0 pushes on this record
a 5 for Func(5,0)
Return Address 50
FCTVAL ?
result 5+Func(5,0) = ?
b 1 record for Func(5,1)
a 5
Return Address 50
FCTVAL ?
result 5+Func(5,1) = ?
b 2 record for Func(5,2)
a 5
Return Address 100
27
Run-Time Stack Activation Records
x = Func(5, 2); // original call at instruction 100
FCTVAL 0
result 0
b 0 record for Func(5,0)
a 5
is popped first
Return Address 50
with its FCTVAL
FCTVAL ?
result 5+Func(5,0) = ?
b 1 record for Func(5,1)
a 5
Return Address 50
FCTVAL ?
result 5+Func(5,1) = ?
b 2 record for Func(5,2)
a 5
Return Address 100
28
Run-Time Stack Activation Records
x = Func(5, 2); // original call at instruction 100
FCTVAL 5
result 5+Func(5,0) = 5+ 0
b 1 record for Func(5,1)
a 5
is popped next
Return Address 50
with its FCTVAL
FCTVAL ?
result 5+Func(5,1) = ?
b 2 record for Func(5,2)
a 5
Return Address 100
29
Run-Time Stack Activation Records
x = Func(5, 2); // original call at line 100
FCTVAL 10
result 5+Func(5,1) = 5+5
b 2 record for Func(5,2)
a 5
is popped last
Return Address 100
with its FCTVAL 30
Show Activation Records for
these calls
x = Func( - 5, - 3 );
x = Func( 5, - 3 );
31
Tail Recursion
The case in which a function
contains only a single recursive call
and it is the last statement to be
executed in the function.
32
// USES TAIL RECURSION
bool ValueInList ( ListType list , int value , int startIndex )
35
Using quick sort algorithm
A..Z
A..L M..Z
36
Before call to function Split
splitVal = 9
GOAL: place splitVal in its proper position with
all values less than or equal to splitVal on its left
and all larger values on its right
9 20 6 10 14 3 60 11
values[first] [last]
37
After call to function Split
splitVal = 9
smaller values larger values
6 3 9 10 14 20 60 11
38
// Recursive quick sort algorithm