SlideShare a Scribd company logo
Recursion
Chapter 17
17
What is recursion?
Recursion occurs when a method calls
itself, either directly or indirectly.
Used to solve difficult, repetitive
problems
17
Factorial Example
N! (N Factorial)
N! = N * (N – 1)! for N >= 0 and 0! = 1
5! = 5 * 4! or 5 * 4 * 3 * 2 * 1 * 1 = 120
4! = 4 * 3! or 4 * 3 * 2 * 1 * 1 = 24
3! = 3 * 2! or 3 * 2 * 1 * 1 = 6
2! = 2 * 1! or 2 * 1 * 1 = 2
1! = 1 * 0! or 1 * 1 = 1
0! = 1
17
Calculate Factorials Using Recursion
The Java solution:
private int doFactorial(int N)
{
if ( N == 0 )
{
return 1;
}
return N * doFactorial(N – 1);
}
17
Towers of Hanoi
Three towers hold rings stacked in order
from largest to smallest (bottom to top)
Player can only move one ring at a time
Larger rings cannot be stacked on top of
smaller ring
Player must move stack to another tower
17
The Towers of Hanoi Algorithm
Classic problem can be solved with
recursion.
Move N–1 rings to storage tower
Move Nth ring to destination tower
Move N–1 rings to destination tower
17
Towers of Hanoi Solution
Final graphical solution includes the files:
TowerRing.java
Tower.java
TowerMove.java
ThreeTowers.java
TowersOfHanoi.java
TowerPanel.java
17
Graphical Towers of Hanoi
Applet in progress

More Related Content

DOCX
Rubik 3x3 Solution for Personal Use
PDF
Recursion examples
PPTX
3.2 Derivative as a Function
DOC
Finite difference
DOC
MATH: ONE STEP EQUATIONS
PDF
11/1 - HW question #7
PPT
Chapter 12
Rubik 3x3 Solution for Personal Use
Recursion examples
3.2 Derivative as a Function
Finite difference
MATH: ONE STEP EQUATIONS
11/1 - HW question #7
Chapter 12

Viewers also liked (7)

PPT
Chapter 18
PPT
Chapter 15
PPT
Enterprise Skills
PPT
Chapter 14
PPT
Chapter 16
PPT
Key enterprise skills
PPT
Chapter 11
Chapter 18
Chapter 15
Enterprise Skills
Chapter 14
Chapter 16
Key enterprise skills
Chapter 11
Ad

Similar to Chapter 17 (20)

PPTX
Recursion in data structure
PDF
recursion2
PDF
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
PDF
Week11
PDF
Recursion - Computer Algorithms
PPT
Ap Power Point Chpt8
PPT
Lecture9 recursion
PPTX
Data structure
PPT
M251_Meeting 9 (Recursion_AdvancedJava).ppt
PPT
Lec-6 Recursion of Data Structures & Algorithms
PDF
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
PPTX
Recursion And Implementation C Programming
PPTX
WT-Pravesh Sakhare.pptx
PPTX
M2-Recursion.pptx
PDF
recursion1
PDF
Recursion
PPT
Ch10 Recursion
PPTX
recursion_esccccccccccc1111122222333.pptx
PPTX
Lecture09 recursion
PPT
Tower of honoi using open source code python
Recursion in data structure
recursion2
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
Week11
Recursion - Computer Algorithms
Ap Power Point Chpt8
Lecture9 recursion
Data structure
M251_Meeting 9 (Recursion_AdvancedJava).ppt
Lec-6 Recursion of Data Structures & Algorithms
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
Recursion And Implementation C Programming
WT-Pravesh Sakhare.pptx
M2-Recursion.pptx
recursion1
Recursion
Ch10 Recursion
recursion_esccccccccccc1111122222333.pptx
Lecture09 recursion
Tower of honoi using open source code python
Ad

More from Graham Royce (20)

PPT
Chapter 13
PPT
Chapter 10
PPT
Chapter 09
PPT
Chapter 08
PPT
Chapter 07
PPT
Chapter 06
PPT
Chapter 05
PPT
Chapter 04
PPT
Chapter 03
PPT
Chapter 02
PPT
Chapter 01
PPT
13 java in oracle
PPT
Java tut1 Coderdojo Cahersiveen
PPT
My 3 min pitch pack
PPTX
Things to do
PPTX
House in order
PPT
Blank 10 all you need
PPTX
PPT
Idea's1 only
PPT
New frontiers motivation
Chapter 13
Chapter 10
Chapter 09
Chapter 08
Chapter 07
Chapter 06
Chapter 05
Chapter 04
Chapter 03
Chapter 02
Chapter 01
13 java in oracle
Java tut1 Coderdojo Cahersiveen
My 3 min pitch pack
Things to do
House in order
Blank 10 all you need
Idea's1 only
New frontiers motivation

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Cloud computing and distributed systems.
PDF
Empathic Computing: Creating Shared Understanding
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
A Presentation on Artificial Intelligence
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Electronic commerce courselecture one. Pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
KodekX | Application Modernization Development
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Big Data Technologies - Introduction.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Cloud computing and distributed systems.
Empathic Computing: Creating Shared Understanding
Network Security Unit 5.pdf for BCA BBA.
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
A Presentation on Artificial Intelligence
NewMind AI Weekly Chronicles - August'25 Week I
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Understanding_Digital_Forensics_Presentation.pptx
Electronic commerce courselecture one. Pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
KodekX | Application Modernization Development
Mobile App Security Testing_ A Comprehensive Guide.pdf
Review of recent advances in non-invasive hemoglobin estimation
The Rise and Fall of 3GPP – Time for a Sabbatical?
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Encapsulation_ Review paper, used for researhc scholars
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

Chapter 17

  • 2. 17 What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive problems
  • 3. 17 Factorial Example N! (N Factorial) N! = N * (N – 1)! for N >= 0 and 0! = 1 5! = 5 * 4! or 5 * 4 * 3 * 2 * 1 * 1 = 120 4! = 4 * 3! or 4 * 3 * 2 * 1 * 1 = 24 3! = 3 * 2! or 3 * 2 * 1 * 1 = 6 2! = 2 * 1! or 2 * 1 * 1 = 2 1! = 1 * 0! or 1 * 1 = 1 0! = 1
  • 4. 17 Calculate Factorials Using Recursion The Java solution: private int doFactorial(int N) { if ( N == 0 ) { return 1; } return N * doFactorial(N – 1); }
  • 5. 17 Towers of Hanoi Three towers hold rings stacked in order from largest to smallest (bottom to top) Player can only move one ring at a time Larger rings cannot be stacked on top of smaller ring Player must move stack to another tower
  • 6. 17 The Towers of Hanoi Algorithm Classic problem can be solved with recursion. Move N–1 rings to storage tower Move Nth ring to destination tower Move N–1 rings to destination tower
  • 7. 17 Towers of Hanoi Solution Final graphical solution includes the files: TowerRing.java Tower.java TowerMove.java ThreeTowers.java TowersOfHanoi.java TowerPanel.java
  • 8. 17 Graphical Towers of Hanoi Applet in progress