0% found this document useful (0 votes)
30 views

Recursion

Programming with Java

Uploaded by

wed77862
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Recursion

Programming with Java

Uploaded by

wed77862
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Recursion

Chapter 11

Prepared by: Areej Althubaity

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

• A recursive algorithm will have one subtask that is a small version of


the entire algorithm's task

• A recursive algorithm contains an invocation of itself

• Must be defined correctly else algorithm could call itself forever or


not at all

• In Java, we can use a recursive algorithm to write a recursive method


in Java

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.

• In the provided example:


• Normal method call is where we have called the
recurse() method from inside the main method

• Recursive method call/invocation is where we are


calling the same recurse method once again inside
itself!
• So how can we stop the recursive call?
• As with loop, we need to provide some conditions
inside the method. Otherwise, the method will be
called infinitely.

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

• Recursive call must eventually terminate

• A base case does not contain a recursive call


• stops the recursion
• A recursive method must have at least one base case!

• Each successive call to itself must be a "smaller version of itself”


• An argument that describes a smaller problem
• A base case that should be eventually reached

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

• First handle the simplest case; the base case or stopping


condition
• The simplest input possible is when the input num is less than 1.
In this case, all we need to do is print a blank line

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!

• To solve this recursively, we have to break the problem up


into a smaller version of the same problem

• This means rephrasing the problem in terms of a method


call back to countdown

• Let’s say that num equals 3. We have invoked


countDown(3) and we want to output “321”.

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

• But, what if we want to


display 2 or 100 lines! Calling
threeLine method wouldn’t
be that helpful!

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.

• The structure is similar to countdown.


As long as n is greater than 0, it displays
a newline and then invokes itself to
display (n − 1) additional newlines.

• The total number of newlines is


1 + (n − 1), which is just what we
wanted: n

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

• Let's consider a method which receives an integer parameter, then it


prints the digits of the number as words

• Example, if the argument is the number 223, the method should


display:
two two three

• The heading of our method can be

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

• We do NOT need to do recursion method if the number is only a


single digit
• we can use a long switch statement to decide which word to use for the digit

• Consider this useful private method


• Why this method is private?!

• Thus, when digit is 0, getWordFromDigit returns "zero", when digit is


1, the method returns "one", etc

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

• If number has multiple digits, decompose algorithm into two


subtasks
1. Display all digits but the last one as words
2. Display last digit as a word

• First subtask is smaller version of original problem


• Same as original task, one less digit

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:

• Which leads to the following:

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

• Must have a branching statement that leads to different cases

• One or more of the branches should have a recursive call of the


method
• Recursive call must us "smaller" version of the original argument

• One or more branches must include no recursive call


• This is the base or stopping case

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

• Nothing stops the method from repeatedly invoking itself


• Program will eventually crash when computer exhausts its resources (stack
overflow)
• stack refers to a data structure that is used to keep track of recursive calls
• The system maintains a record within the computer’s memory of each
recursive call
• Method begins: add data onto the stack
• Method ends: remove data from the stack

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

All recursive algorithms/methods


can be rewritten without recursion.

• Any method including a recursive call can be rewritten


• To do the same task
• Done without recursion

• Non recursive algorithm uses iteration


• Method which implements is iterative method

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

• Iterative methods use loops instead of recursion


• They are generally run faster and use less memory
• Less overhead in keeping track of method calls

• Recursive methods
• Uses more storage space than iterative version
• Due to overhead during runtime
• Also runs slower

• However, in some programming tasks, recursion is a better choice, a


more elegant solution

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?

• Solutions/algorithms for some problems are inherently recursive


• iterative implementation could be more complicated

• When efficiency is less important


• it might make the code easier to understand

• Bottom line is about:


• Algorithm design
• Tradeoff between readability and efficiency

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

• Follow same design guidelines as stated previously (Slide# 28)

• Second guideline also states


• One or more branches includes recursive invocation that leads to the
returned 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

• Note recursive method


NumberOfZeros
• Has two recursive calls
• Each returns value assigned to
result
• Variable result is what is
returned

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

• Insisting that user input be


correct
• Program asks for a input in
specific range
• Recursive method makes sure
of this range
• Method recursively invokes
itself as many times as user
gives incorrect input
• Dangerous technique – can
result in stack overflow if
invalid entries entered
repeatedly

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

• The factorial of an integer n, which is written n!, is defined as


follows:
0! = 1
n! = n · (n − 1)!

• This means factorial(0) is 1, and factorial(n) is n * factorial(n - 1).


• factorial(3) is 3 * factorial(2);
• factorial(2) is 2 * factorial(1);
• factorial(1) is 1 * factorial(0);
• and factorial(0) is 1.
• Putting it all together, we get 3 * 2 * 1 * 1, which is 6.

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!)

• N! = (N-1)! * N [for N > 1]


• 1! = 1
• 3!
= 2! * 3
= (1! * 2) * 3
=1*2*3

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

• The first step is to decide what the parameters and return


type are.

• Since factorial is defined for integers, the method takes an


int as a parameter and returns an int

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

• Next, we think about the base case.


• If the argument happens to be 0, we
return 1:

• Otherwise, we have to make a


recursive call to find the factorial of n −
1, and then multiply it by n:

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

public static int factorial(int n)


{
int fact;
if (n > 1) // recursive case (decomposition)
fact = factorial(n – 1) * n; // composition
else // base case
fact = 1;

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;
}

public static int factorial(int 2)


{
int fact;
if (n > 1)
fact = factorial(1) * 2;
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;
}

public static int factorial(int 2)


{
int fact;
if (n > 1)
fact = factorial(1) * 2;
else
fact = 1;
return fact;
}

public static int factorial(int 1)


{
int fact;
if (n > 1)
fact = factorial(n - 1) * n;
else
fact = 1;
return fact;
} th
JAVA: An Introduction to Problem Solving & Programming, 8 Ed. By Walter Savitch 40
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;
}

public static int factorial(int 2)


{
int fact;
if (n > 1)
fact = factorial(1) * 2;
else
fact = 1;
return fact;
}

public static int factorial(int 1)


{
int fact;
if (n > 1)
fact = factorial(n - 1) * n;
else
fact = 1;
return 1;
} th
JAVA: An Introduction to Problem Solving & Programming, 8 Ed. By Walter Savitch 41
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;
}

public static int factorial(int 2)


{
int fact;
if (n > 1)
fact = 1 * 2;
else
fact = 1;
return fact;
}

public static int factorial(int 1)


{
int fact;
if (n > 1)
fact = factorial(n - 1) * n;
else
fact = 1;
return 1;
} th
JAVA: An Introduction to Problem Solving & Programming, 8 Ed. By Walter Savitch 42
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;
}

public static int factorial(int 2)


{
int fact;
if (n > 1)
fact = 1 * 2;
else
fact = 1;
return 2;
}

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;
}

public static int factorial(int 2)


{
int fact;
if (n > 1)
fact = 1 * 2;
else
fact = 1;
return 2;
}

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)

(decomposition) fact = factorial(n – 1) * n; (composition)


else // base case
fact = 1;
return fact;
}

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)

of a Number fact = factorial(n – 1) * n; (composition)


else // base case
fact = 1;
return fact;
}

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)

of a Number fact = factorial(n – 1) * n; (composition)


else // base case
fact = 1;
return fact;
}

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)

of a Number fact = factorial(n – 1) * n; (composition)


else // base case
fact = 1;
return fact;
}

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)

of a Number fact = factorial(n – 1) * n; (composition)


else // base case
fact = 1;
return fact;
}

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)

of a Number fact = factorial(n – 1) * n; (composition)


else // base case
fact = 1;
return fact;
}

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)

of a Number fact = factorial(n – 1) * n; (composition)


else // base case
fact = 1;
return fact;
}

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

• The Nth Fibonacci number is the sum of the


previous two 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

public static int fibonacci(int n)


{
int fib;
if (n > 2)
fib = fibonacci(n-1) + fibonacci(n-2);
else if (n == 2)
fib = 1;
else
fib = 0;
return fib;
}
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 55
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

• Method with self invocation


• Invocation considered a recursive call

• 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy