Professional Documents
Culture Documents
Recursion: CSIS1117 Computer Programming
Recursion: CSIS1117 Computer Programming
Recursion: CSIS1117 Computer Programming
Recursion
c1117 lecture 11
Recursion
If n equals to 0, y0 = 1
If n equals to 1, y1 = y y0
If n equals to 2, y2 = y y1
If n equals to k, yk = y yk-1
Finishing the sub-problem yk-1 can help to solve the
original problem yk.
c1117 lecture 11
Recursion
E.g. n equals to 0, y0 = 1
c1117 lecture 11
Example 1
Base case:
if(n == 0) return 1;
Recursive case:
if(n > 0) return y * exp(y, n-1);
c1117 lecture 11
Example 1
The base case
double exp(double y, int n){
if (n == 0)
The recursive case
return 1;
return y * exp(y, n-1);
}
See exp.cc
c1117 lecture 11
Example 1
2
exp(2,3);
c1117 lecture 11
Example 2
n! = n (n 1) 3 2 1
Design a function factorial(n) to compute the nfactorial
int factorial(int n);
c1117 lecture 11
an empty list; or
a node followed by a (shorter) linked list with one node
lesser.
mylist
dog
A node
c1117 lecture 11
cat
bat
ant
Example 3
c1117 lecture 11
10
ListLength(mylist);
ListLength(mylist);
ptr
mylist
int
int ListLength(Node
ListLength(Node ** ptr){
ptr){
if(ptr
if(ptr ==
== 0)
0) return
return 0;
0;
return
return 11 ++ ListLength(ptr->next);
ListLength(ptr->next);
}}
ptr
3 int
int ListLength(Node
ListLength(Node ** ptr){
ptr){
if(ptr
if(ptr ==
== 0)
0) return
return 0;
0;
return
return 11 ++ ListLength(ptr->next);
ListLength(ptr->next);
}}
ptr
2 int
int ListLength(Node
ListLength(Node ** ptr){
ptr){
if(ptr
if(ptr ==
== 0)
0) return
return 0;
0;
return
return 11 ++ ListLength(ptr->next);
ListLength(ptr->next);
}}
ptr
int ListLength(Node
ListLength(Node ** ptr){
ptr){
1 int
if(ptr
if(ptr ==
== 0)
0) return
return 0;
0;
return
return 11 ++ ListLength(ptr->next);
ListLength(ptr->next);
}}
0
ptr
int
int ListLength(Node
ListLength(Node ** ptr){
ptr){
if(ptr
if(ptr ==
== 0)
0) return
return 0;
0;
return
return 11 ++ ListLength(ptr->next);
ListLength(ptr->next);
}}
c1117 lecture 11
dog
cat
bat
ant
11
Example 4
c1117 lecture 11
12
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
PrintList(ptr->next);
PrintList(ptr->next);
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
PrintList(ptr->next);
PrintList(ptr->next);
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
PrintList(ptr->next);
PrintList(ptr->next);
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
PrintList(ptr->next);
PrintList(ptr->next);
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){
ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
PrintList(ptr->next);
PrintList(ptr->next);
c1117 lecture}
}11
PrintList(mylist);
PrintList(mylist);
mylist
dog
cat
dog
cat
bat
ant
bat
ant
13
c1117 lecture 11
14
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
PrintList(ptr->next);
PrintList(ptr->next);
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
}} void PrintList(Node * ptr){
void PrintList(Node * ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
PrintList(ptr->next);
PrintList(ptr->next);
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
PrintList(ptr->next);
PrintList(ptr->next);
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
PrintList(ptr->next);
PrintList(ptr->next);
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){
ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
PrintList(ptr->next);
PrintList(ptr->next);
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
c1117 lecture}
}11
PrintList(mylist);
PrintList(mylist);
mylist
dog
cat
ant
bat
cat
ant
bat
ant
15
Example on Array
A element
c1117 lecture 11
10
18 15
30 21
16
Example on Array
Can we do better?
17
Recursive vs iterative
c1117 lecture 11
18