Recursion

advertisement
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
Recursion [ Why is it important?]
•
Strengthen Top-down Thinking
•
Further Big Jump in programming
Get Mature in
- Setting parameters
- Method calls
- ‘return’ + work with ‘return value’
•
Required in some other core courses
brainwashing
first!
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// return the largest digit in a positive integer n
static int getLargestDigit(int n)
{
Figure out the return type and signature and of the method
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// return the largest digit in a positive integer n
static int getLargestDigit(int n)
{
if (n<10)
The easiest case(s) which doesn’t need recursion
{
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// return the largest digit in a positive integer n
static int getLargestDigit(int n)
{
if (n<10)
Handle the easiest case(s) -- doesn’t need recursion
{
return n;
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// return the largest digit in a positive integer n
static int getLargestDigit(int n)
{
if (n<10)
{
return n;
}
else
{
 Recursion
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// return the largest digit in a positive integer n
static int getLargestDigit(int n)
{
if (n<10)
{
return n;
}
else
Recursive call
{
a reduced problem: n/10
 Recursion
}
}
getLargesetDigit(
);
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// return the largest digit in a positive integer n
static int getLargestDigit(int n)
{
if (n<10)
{
return n;
}
else
Recursive call
{
(Has return value => save in a variable)
int m1;
 Recursion
m1=getLargesetDigit(n/10);
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// return the largest digit in a positive integer n
static int getLargestDigit(int n)//56732 => 7
{
if (n<10)
 Illustrate with
{
example for
return n;
further planning.
}
else
{
int m1;
 Recursion
m1=getLargesetDigit(n/10); //5673 => 7
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// return the largest digit in a positive integer n
static int getLargestDigit(int n)//56732 => 7
{
if (n<10)
 Illustrate with
{
example for
return n;
further planning.
}
else
{
int m1;
 Recursion
m1=getLargesetDigit(n/10); //5673 => 7
}
}
Now, m1 and n%10
(ie. 7 and 2) can decide the final result.
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// return the largest digit in a positive integer n
static int getLargestDigit(int n)//56732 => 7
{
if (n<10)
 Illustrate with
{
example for
return n;
further planning.
}
else
{
int m1;
 Recursion
m1=getLargesetDigit(n/10); //5673 => 7
if ...
return m1;
else
Now, m1 and n%10
return n%10;
(ie. 7 and 2) can }
decide the final result.
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
A Simple Summary
 Call helper robot 
“first”
Planning
“next”
Illustrate with
example for
the planning.
// return the largest digit in a positive integer n
static int getLargestDigit(int n)//56732 => 7
{
if (n<10)
{
return n;
}
else
{
int m1;
m1=getLargesetDigit(n/10);//5673 => 7
if ...
return m1;
else
return n%10;
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
// return the largest digit in a positive integer n
static int getLargestDigit(int n)//56732 => 7
{ X
if (n<10)
{
* Do not add any code here (like
return n;
int m1, return ..).
}
else
Reason: the if-part and else-part have different
logic. If we force them to share common code,
{
then we add confusing complexity rather than
int m1;
giving significant advantage.
m1=getLargesetDigit(n/10);//5673 => 7
if ...
return m1;
else
return n%10;
}
X
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
// return the largest digit in a positive integer n
static int getLargestDigit(int n)
{
• Parameters  caller tells info for the job
no more (count,largeDigit), no less !!
• Avoid Excess parameters, Global variables
They often COMPLICATE the job and CREATE ERRORS.
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x)
{
Figure out the return type and signature and of the method
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x)
{
if (x<10)
The easiest case(s) which doesn’t need recursion
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x)
{
if (x<10)
Handle the easiest case(s) -- doesn’t need recursion
{
if (x%2==0)
return true;
else
return false;
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x)
{
Simplify from:
if (x<10)
if (x%2==0)
return true;
{
else
return (x%2==0);
return false;
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x)
{
if (x<10)
{
return (x%2==0);
}
else
Recursive call
{
(Has return value => save in a variable)
 Recursion
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x)
{
if (x<10)
{
return (x%2==0);
}
else
{
 Recursion bool m1=containEven(x/10);
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x) //56732 => true
{
if (x<10)
 Illustrate with example
{
for further planning.
return (x%2==0);
}
else
{
 Recursion bool m1=containEven(x/10); //5673 => true
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x) //56732 => true
{
if (x<10)
 Illustrate with example
{
for further planning.
return (x%2==0);
}
else
{
 Recursion bool m1=containEven(x/10); //5673 => true
}
}
Now, m1 tells true/false about the leading digits. Only n%10 has not been checked yet. Therefore, using m1 and n%10 , we can decide the final result.
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x) //56732 => true
{
if (x<10)
 Illustrate with example
{
for further planning.
return (x%2==0);
}
else
{
 Recursion bool m1=containEven(x/10); //5673 => true
if (x%2==0)
return true;
else
return m1;
}
}
Now, m1 tells true/false about the leading digits. Only n%10 has not been checked yet. Therefore, using m1 and n%10 , we can decide the final result.
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
A Simple Summary
 Call helper robot 
“first”
Planning
“next”
Illustrate with
example for
the planning.
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x)
{
if (x<10)
{
return (x%2==0);
}
else
{
bool m1=containEven(x/10);
if (x%2==0)
return true;
else
return m1;
}
}
//56732 => true
//5673 => true
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x)
{ X
if (x<10)
{
* Do not add any code here (like
return (x%2==0);
int m1, return ..).
}
Reason: the if-part and else-part have different
else
logic. If we force them to share common code,
{
then we add confusing complexity rather than
bool m1=containEven(x/10);
giving significant advantage.
if (x%2==0)
return true;
else
return m1;
}
}
X
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x)
{
• Parameters  caller tells info for the job
no more (count,largeDigit), no less !!
• Avoid Excess parameters, Global variables
They often COMPLICATE the job and CREATE ERRORS.
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
Or we can write the recursive part as:
Then simply eliminate m1:
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
// determine whether an integer x contains even digit(s) (0,2,4,6,8)
static boolean containEven(int x)
{
if (x<10)
{
return (x%2==0);
}
else
{
if (x%2==0)
return true;
else
{
bool m1=containEven(x/10) ;
return m1;
}
}
}
static boolean containEven(int x)
{
if (x<10)
{
return (x%2==0);
}
else
{
if (x%2==0)
return true;
else
return containEven(x/10);
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// show the digits of an integer x reversely (each followed by a space)
static void showDigitsReverse(int x)
{
Figure out the return type and signature and of the method
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// show the digits of an integer x reversely (each followed by a space)
static void showDigitsReverse(int x)
{
if (x<10)
The easiest case(s) which doesn’t need recursion
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// show the digits of an integer x reversely (each followed by a space)
static void showDigitsReverse(int x)
{
if (x<10)
Handle the easiest case(s) -- doesn’t need recursion
{
System.out.print(x + " ");
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// show the digits of an integer x reversely (each followed by a space)
static void showDigitsReverse(int x)
{
if (x<10)
{
System.out.print(x + " ");
}
else
{
Recursive call
(no return value here)
 Recursion
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// show the digits of an integer x reversely (each followed by a space)
static void showDigitsReverse(int x)
{
if (x<10)
{
System.out.print(x + " ");
}
else
{
Recursive call
(no return value here)
 Recursion
showDigitsReverse(x/10);
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// show the digits of an integer x reversely (each followed by a space)
static void showDigitsReverse(int x) //56732 => 2 3
{
if (x<10)
 Illustrate with example
{
for further planning.
System.out.print(x + " ");
}
else
{
 Recursion
showDigitsReverse(x/10); //5673 => 3 7 6 5
}
}
7 6 5
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// show the digits of an integer x reversely (each followed by a space)
static void showDigitsReverse(int x) //56732 => 2 3 7 6 5
{
if (x<10)
 Illustrate with example
{
for further planning.
System.out.print(x + " ");
}
else
{
 Recursion
showDigitsReverse(x/10); //5673 => 3 7 6 5
}
}
In the code, the recursive call can output 3 7 6 5 .
Only n%10 has not been handled yet. Since the required output is 2 3 7 6 5, we can easily decide what else is to be done and when.
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
 Structure + Base case
// show the digits of an integer x reversely (each followed by a space)
static void showDigitsReverse(int x) //56732 => 2 3 7 6 5
{
if (x<10)
 Illustrate with example
{
for further planning.
System.out.print(x + " ");
}
else
{
 Recursion System.out.print( x%10 + " " );
showDigitsReverse(x/10); //5673 => 3 7 6 5
}
}
In the code, the recursive call can output 3 7 6 5 .
Only n%10 has not been handled yet. Since the required output is 2 3 7 6 5, we can easily decide what else is to be done and when.
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
A Simple Summary
 Call helper robot 
“first”
Planning
“next”
Illustrate with
example for
the planning.
// show the digits of an integer x reversely (each followed by a space)
static void showDigitsReverse(int x) //56732 => 2 3 7 6 5
{
if (x<10)
{
cout << x << " ";
}
else
{
cout << x%10;
showDigitsReverse(x/10); //5673 => 3 7 6 5
}
}
[Lab05 Recursion] Introduction => Q2 getLargestDigit => Q3 containEven => Q4 showDigitsReverse
// show the digits of an integer x reversely (each followed by a space)
static void showDigitsReverse(int x)
{
• Parameters  caller tells info for the job
no more (count,largeDigit), no less !!
• Avoid Excess parameters, Global variables
They often COMPLICATE the job and CREATE ERRORS.
}
Download