Recursion
Recursion
Chapter 11
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 1
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Recursive Algorithm
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 2
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Recursive Methods
• Recursive method is when a method calls itself.
• This process is known as recursion.
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 3
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Recursive Methods
• A recursive method is one whose definition contains an
invocation of itself
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 4
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Recursive Void Methods
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 5
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Simple Example - Countdown
• Given an integer value num output all the numbers from
num down to 1
• Can do this easier and faster with a loop; the recursive version is
an example only
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 6
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Simple Example - Countdown
• This method handles the base case, but not the more
interesting case when num is 1 or more!
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 7
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Recursive
Countdown-
Sequence of Calls
of
countDown(3)
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 8
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example - Recursive Display New Lines
• We could manage
calling/invoking newline ()
three times by just calling
threeLine method
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 9
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example - Recursive Display New Lines
• A more general way is to define nLines
method that takes an integer, n, as a
parameter and displays n newlines.
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 10
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example - Digits to Words
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 11
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Digits to Words
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 12
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Digits to Words
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 13
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Digits to Words
• How do we remove the last digit of a number so we can perform step
1 yet retain that digit for step 2?
• ِExample:
• Suppose we choose a number that has more than one digit, e.g. 987
• So, to break this number into 98 and 7, we can use integer division by 10
(987/ 10 is 98), and (987% 10 is 7)
• Thus, our algorithm leads us to the following Java code:
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 14
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Digits to Words
• Which leads to the following:
• But still we need to make a special case for numbers that are only
one digit long! Thus the correct method’s implementation is:
base case
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 15
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Digits to
Words (Full Program)
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 16
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
How Recursion Works
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 17
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
How Recursion Works
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 18
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
How Recursion Works
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 19
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
How Recursion Works
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 20
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
How Recursion Works
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 21
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Keys to Successful Recursion
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 22
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Infinite Recursion
• Suppose we leave out the stopping case
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 23
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Recursive Versus Iterative Methods
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 24
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Recursive Versus Iterative Methods
• Recursive methods
• Uses more storage space than iterative version
• Due to overhead during runtime
• Also runs slower
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 25
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
So When Should You Use Recursion?
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 26
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Recursive Methods that Return a Value
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 27
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Recursive Methods that Return a Value
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 28
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Number of Zeros in an Integer
• Example,
getNumberOfZeros(2030)
returns 2, because 2030
contains two zero digits
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 29
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int numberOfZeros(int n)
{
Example- Number int zeroCount;
if (n==0)
of Zeros in an zeroCount = 1;
else if (n < 10) // and not 0
Integer zeroCount = 0; // 0 for no zeros
else if (n%10 == 0)
zeroCount = numberOfZeros(n/10) + 1;
else // n%10 != 0
zeroCount = numberOfZeros(n/10);
return zeroCount;
}
numberOfZeros(2008)
numberOfZeros(200) 5
numberOfZeros(20) 0
numberOfZeros(2) 0
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 30
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Number
of Zeros in an
Integer
numberOfZeros(2008)->2
+
numberOfZeros(200)->2 8->0
+
numberOfZeros(20)->1 0->1
+
numberOfZeros(2)->0 0->1
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 31
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Programming Example
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 32
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example-Factorial of a Number
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 33
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Factorial (N!)
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 34
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example-Factorial of a Number
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 35
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example-Factorial of a Number
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 36
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
factorial Method
return fact;
}
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 37
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int factorial(int 3)
{
int fact;
if (n > 1)
fact = factorial(2) * 3;
else
fact = 1;
return fact;
}
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 38
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int factorial(int 3)
{
int fact;
if (n > 1)
fact = factorial(2) * 3;
else
fact = 1;
return fact;
}
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 39
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int factorial(int 3)
{
int fact;
if (n > 1)
fact = factorial(2) * 3;
else
fact = 1;
return fact;
}
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 43
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int factorial(int 3)
{
int fact;
if (n > 1)
fact = 2 * 3;
else
fact = 1;
return fact;
}
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 44
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int factorial(int 3)
{
int fact;
if (n > 1)
fact = 2 * 3;
else
fact = 1;
return 6;
}
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 45
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int factorial(int n)
{
Execution Trace int fact;
if (n > 1) // recursive case (decomposition)
factorial(4)
factorial(3) 4
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 46
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int factorial(int n)
{
Example-Factorial int fact;
if (n > 1) // recursive case (decomposition)
factorial(4)
factorial(3) 4
factorial(2) 3
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 47
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int factorial(int n)
{
Example-Factorial int fact;
if (n > 1) // recursive case (decomposition)
factorial(4)
factorial(3) 4
factorial(2) 3
factorial(1) 2
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 48
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int factorial(int n)
{
Example-Factorial int fact;
if (n > 1) // recursive case (decomposition)
factorial(4)
*
factorial(3) 4
*
factorial(2) 3
*
factorial(1)->1 2
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 49
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int factorial(int n)
{
Example-Factorial int fact;
if (n > 1) // recursive case (decomposition)
factorial(4)
*
factorial(3) 4
*
factorial(2)->2 3
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 50
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int factorial(int n)
{
Example-Factorial int fact;
if (n > 1) // recursive case (decomposition)
factorial(4)
*
factorial(3)->6 4
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 51
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
public static int factorial(int n)
{
Example-Factorial int fact;
if (n > 1) // recursive case (decomposition)
factorial(4)->24
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 52
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
More Examples on Recursion
FYI
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 53
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Fibonacci Numbers
0, 1, 1, 2, 3, 5, 8, 13, …
• Base case:
• fibonacci(1) = 0
• fibonacci(2) = 1
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 54
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Fibonacci Numbers
fibonacci(4)
fibonacci(3) fibonacci(2)
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 56
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Fibonacci Numbers
fibonacci(4)
fibonacci(3) fibonacci(2)
fibonacci(2) fibonacci(1)
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 57
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Fibonacci Numbers
fibonacci(4)
+
fibonacci(3) fibonacci(2)
+
fibonacci(2)->1 fibonacci(1)->0
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 58
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Fibonacci Numbers
fibonacci(4)
+
fibonacci(3)->1 fibonacci(2)->1
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 59
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Example- Fibonacci Numbers
fibonacci(4)->2
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 60
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Summary
• Recursive calls
• Legal in Java
• Can make some method definitions clearer
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 61
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Reading
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 62
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved