Recursion
Recursion
What is recursion?
if(x==0)
return 1;
else {
y = 2 * f(x-1);
return y+1;
}
}
Problems defined recursively
There are many problems whose solution can be defined recursively
Example: n factorial
1 if n = 0
n!= (recursive solution)
(n-1)!*n if n > 0
1 if n = 0
n!= (closed form solution)
1*2*3*…*(n-1)*n if n > 0
Coding the factorial function
Recursive implementation
int Factorial(int n)
{
if (n==0) // base case
return 1;
else
return n * Factorial(n-1);
}
Coding the factorial function
(cont.)
Iterative implementation
int Factorial(int n)
{
int fact = 1;
return fact;
}
Another example:
n choose k (combinations)
Given n things, how many different sets of size k can be chosen?
n n-1 n-1
= + , 1 < k < n (recursive solution)
k k k-1
n n!
= , 1 < k < n (closed-form solution)
k k!(n-k)!
with base cases:
n n
= n (k = 1), = 1 (k = n)
1 n
n choose k (combinations)
template<class ItemType>
void SortedType<ItemType>::RetrieveItem(ItemType& item, bool& found)
{
int midPoint;
int first = 0;
int last = length - 1;
found = false;
while( (first <= last) && !found) {
midPoint = (first + last) / 2;
if (item < info[midPoint])
last = midPoint - 1;
else if(item > info[midPoint])
first = midPoint + 1;
else {
found = true;
item = info[midPoint];
}
}
}
Recursive binary search (cont’d)
What is the size factor?
The number of elements in (info[first] ...
info[last])
template<class ItemType>
void SortedType<ItemType>::RetrieveItem
(ItemType& item, bool& found)
{
found = BinarySearch(info, item, 0, length-1);
}
How is recursion
implemented?
What happens when a function gets called?
int a(int w)
{
return w+w;
}
int b(int x)
{
int z,y;
……………… // other statements
z = a(x) + y;
return z;
}
What happens when a
function is called? (cont.)
An activation record is stored into a stack
(run-time stack)
1) The computer has to stop executing function b
and starts executing function a
2) Since it needs to come back to function b later,
it needs to store everything about function b
that is going to need (x, y, z, and the place to
start executing upon return)
3) Then, x from a is bounded to w from b
4) Control is transferred to function a
What happens when a
function is called? (cont.)
After function a is executed, the activation record is popped out of
the run-time stack
All the old values of the parameters and variables in function b are
restored and the return value of function a replaces a(x) in the
assignment statement
What happens when a
recursive function is called?
Except the fact that the calling and called functions
have the same name, there is really no difference
between recursive and nonrecursive calls
int f(int x)
{
int y;
if(x==0)
return 1;
else {
y = 2 * f(x-1);
return y+1;
}
}
2*f(2)
2*f(1)
2*f(1)
=f(0)
=f(1)
=f(2)
=f(3)
Recursive InsertItem (sorted list)
location
location
location
location
Recursive InsertItem (sorted list)
What is the size factor?
The number of elements in the current list
What is the base case(s)?
1) If the list is empty, insert item into the empty list
2) If item < location->info, insert item as the first node in the current list
What is the general case?
Insert(location->next, item)
Recursive InsertItem (sorted list)
template <class ItemType>
void Insert(NodeType<ItemType>* &location, ItemType item)
{
if(location == NULL) || (item < location->info)) { // base cases
location
location
Recursive DeleteItem (sorted list)
(cont.)
What is the size factor?
The number of elements in the list
What is the base case(s)?
If item == location->info, delete node pointed by location
What is the general case?
Delete(location->next, item)
Recursive DeleteItem (sorted list)
(cont.)
template <class ItemType>
void Delete(NodeType<ItemType>* &location, ItemType item)
{
if(item == location->info)) {
Comb (6,4)
= 3 + 2 + 1 + 2 + 1 + 1 + 2 + 1 + 1 + 1
= 15
Deciding whether to use a recursive
solution