Documente Academic
Documente Profesional
Documente Cultură
15
Recursion
OBJECTIVES
In this chapter you will learn:
The concept of recursion.
How to write and use recursive methods.
How to determine the base case and recursion step in a
recursive algorithm.
How recursive method calls are handled by the system.
The differences between recursion and iteration, and
when it is appropriate to use each.
What geometric shapes called fractals are and how to
draw them using recursion.
What recursive backtracking is and why it is an
effective problem-solving technique.
15.1 Introduction
15.2 Recursion Concepts
15.3 Example Using Recursion: Factorials
15.4 Example Using Recursion: Fibonacci Series
15.5 Recursion and the Method Call Stack
15.6 Recursion vs. Iteration
15.7 Towers of Hanoi
15.8 Fractals
15.9 Recursive Backtracking
15.10 Wrap-Up
15.11 Internet and Web Resources
15.1 Introduction
Fig. 15.1 | Summary of the 32 recursion examples and exercises in this text.
(Part 1 of 2)
Fig. 15.1 | Summary of the 32 recursion examples and exercises in this text.
(Part 2 of 2)
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
Fibonacci of 0 is: 0
Fibonacci of 1 is: 1
Fibonacci of 2 is: 1
Fibonacci of 3 is: 2
Fibonacci of 4 is: 3
Fibonacci of 5 is: 5
Fibonacci of 6 is: 8
Fibonacci of 7 is: 13
Fibonacci of 8 is: 21
Fibonacci of 9 is: 34
Fibonacci of 10 is: 55
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
Fig. 15.12 | Towers of Hanoi for the case with four disks.
1 --> 3
1 --> 2
3 --> 2
1 --> 3
2 --> 1
2 --> 3
1 --> 3
15.8 Fractals
15.8 Fractals
(c) (d)
(e) (f)
Fig. 15.18 | “Lo fractal” at level 1, with C and D points determined for level 2. [Note: The fractal at level
0 is included as a dashed line as a reminder of where the line was located in relation to the current
fractal.]
Fig. 15.19 | “Lo fractal” at level 2, with dashed lines from level 1 provided.