SlideShare uma empresa Scribd logo
1 de 35
Java Programming: From Problem
Analysis to Program Design, 5e

Chapter 13
Recursion
Chapter Objectives
• Learn about recursive definitions
• Explore the base case and the general case
of a recursive definition
• Learn about recursive algorithms

Java Programming: From Problem Analysis to Program Design, 5e

2
Chapter Objectives (continued)
• Learn about recursive methods
• Become aware of direct and indirect
recursion
• Explore how to use recursive methods to
implement recursive algorithms

Java Programming: From Problem Analysis to Program Design, 5e

3
Recursive Definitions
• Recursion
– Process of solving a problem by reducing it to
smaller versions of itself

• Recursive definition
– Definition in which a problem is expressed in
terms of a smaller version of itself
– Has one or more base cases

Java Programming: From Problem Analysis to Program Design, 5e

4
Recursive Definitions (continued)

Java Programming: From Problem Analysis to Program Design, 5e

5
Recursive Definitions (continued)
• Recursive algorithm
– Algorithm that finds the solution to a given problem
by reducing the problem to smaller versions of itself
– Has one or more base cases
– Implemented using recursive methods

• Recursive method
– Method that calls itself

• Base case
– Case in recursive definition in which the solution is
obtained directly
– Stops the recursion

Java Programming: From Problem Analysis to Program Design, 5e

6
Recursive Definitions (continued)
• General solution
– Breaks problem into smaller versions of itself

• General case
– Case in recursive definition in which a smaller
version of itself is called
– Must eventually be reduced to a base case

Java Programming: From Problem Analysis to Program Design, 5e

7
Tracing a Recursive Method
• Recursive method
– Logically, you can think of a recursive method
having unlimited copies of itself
– Every recursive call has its own:
• Code
• Set of parameters
• Set of local variables

Java Programming: From Problem Analysis to Program Design, 5e

8
Tracing a Recursive Method
(continued)
• After completing a recursive call
– Control goes back to the calling environment
– Recursive call must execute completely before
control goes back to previous call
– Execution in previous call begins from point
immediately following recursive call

Java Programming: From Problem Analysis to Program Design, 5e

9
Recursive Definitions
• Directly recursive: a method that calls itself
• Indirectly recursive: a method that calls
another method and eventually results in the
original method call
• Tail recursive method: recursive method in
which the last statement executed is the
recursive call
• Infinite recursion: the case where every
recursive call results in another recursive call
Java Programming: From Problem Analysis to Program Design, 5e

10
Designing Recursive Methods
• Understand problem requirements
• Determine limiting conditions
• Identify base cases

Java Programming: From Problem Analysis to Program Design, 5e

11
Designing Recursive Methods
(continued)
• Provide direct solution to each base case
• Identify general case(s)
• Provide solutions to general cases in terms
of smaller versions of general cases

Java Programming: From Problem Analysis to Program Design, 5e

12
Recursive Factorial Method
public static int fact(int num)
{
if (num = = 0)
return 1;
else
return num * fact(num – 1);
}

Java Programming: From Problem Analysis to Program Design, 5e

13
Recursive Factorial Method (continued)

Java Programming: From Problem Analysis to Program Design, 5e

14
Largest Value in Array

Java Programming: From Problem Analysis to Program Design, 5e

15
Largest Value in Array (continued)
•
•

if the size of the list is 1
the largest element in the list is the only element in the list
else
to find the largest element in list[a]...list[b]
a. find the largest element in list[a + 1]...list[b]
and call it max
b. compare list[a] and max
if (list[a] >= max)
the largest element in list[a]...list[b] is
list[a]
else
the largest element in list[a]...list[b] is max
Java Programming: From Problem Analysis to Program Design, 5e

16
Largest Value in Array (continued)
public static int largest(int[] list,
int lowerIndex,
int upperIndex)
{
int max;
if (lowerIndex == upperIndex)
return list[lowerIndex];
else
{
max = largest(list, lowerIndex + 1, upperIndex);
if (list[lowerIndex] >= max)
return list[lowerIndex];
else
return max;
}
}
Java Programming: From Problem Analysis to Program Design, 5e

17
Execution of largest
(list, 0, 3)

Java Programming: From Problem Analysis to Program Design, 5e

18
Execution of largest (list, 0, 3)

Java Programming: From Problem Analysis to Program Design, 5e

19
Recursive Fibonacci

Java Programming: From Problem Analysis to Program Design, 5e

20
Recursive Fibonacci (continued)
public static int rFibNum(int a, int b,
int n)
{
if (n == 1)
return a;
else if (n == 2)
return b;
else
return rFibNum(a, b, n -1) +
rFibNum(a, b, n - 2);
}
Java Programming: From Problem Analysis to Program Design, 5e

21
Recursive Fibonacci (continued)

Java Programming: From Problem Analysis to Program Design, 5e

22
Towers of Hanoi Problem with
Three Disks

Java Programming: From Problem Analysis to Program Design, 5e

23
Towers of Hanoi: Three Disk
Solution

Java Programming: From Problem Analysis to Program Design, 5e

24
Towers of Hanoi: Three Disk Solution
(continued)

Java Programming: From Problem Analysis to Program Design, 5e

25
Towers of Hanoi: Recursive Algorithm
public static void moveDisks(int count,
int needle1, int needle3, int needle2)
{
if (count > 0)
{
moveDisks(count - 1, needle1,
needle2, needle3);
System.out.println("Move disk " + count
+ " from needle "
+ needle1 + " to needle "
+ needle3 + ". ");
moveDisks(count - 1, needle2,
needle3, needle1);
}
}
Java Programming: From Problem Analysis to Program Design, 5e

26
Recursion or Iteration?
• Two ways to solve particular problem
– Iteration
– Recursion

• Iterative control structures: use looping to
repeat a set of statements
• Tradeoffs between two options
– Sometimes recursive solution is easier
– Recursive solution is often slower
Java Programming: From Problem Analysis to Program Design, 5e

27
Programming Example:
Decimal to Binary

Java Programming: From Problem Analysis to Program Design, 5e

28
Java Programming: From Problem Analysis to Program Design, 5e

29
Sierpinski Gaskets of Various Orders

Java Programming: From Problem Analysis to Program Design, 5e

30
Programming Example:
Sierpinski Gasket
• Input: nonnegative integer indicating level
of Sierpinski gasket
• Output: triangle shape displaying a
Sierpinski gasket of the given order
• Solution includes:
– Recursive method drawSierpinski
– Method to find midpoint of two points
Java Programming: From Problem Analysis to Program Design, 5e

31
Programming Example:
Sierpinski Gasket (continued)
private void drawSierpinski(Graphics g, int lev,
Point p1, Point p2, Point p3)
{
Point midP1P2;
Point midP2P3;
Point midP3P1;
if (lev > 0)
{
g.drawLine(p1.x, p1.y, p2.x, p2.y);
g.drawLine(p2.x, p2.y, p3.x, p3.y);
g.drawLine(p3.x, p3.y, p1.x, p1.y);
midP1P2 = midPoint(p1, p2);
midP2P3 = midPoint(p2, p3);
midP3P1 = midPoint(p3, p1);
drawSierpinski(g, lev - 1, p1, midP1P2,
midP3P1);
drawSierpinski(g, lev - 1, p2, midP2P3,
midP1P2);
drawSierpinski(g, lev - 1, p3,
midP3P1, midP2P3);
}
} Java Programming: From Problem Analysis to Program Design, 5e

32
Programming Example: Sierpinski
Gasket (continued)

Java Programming: From Problem Analysis to Program Design, 5e

33
Chapter Summary
•
•
•
•
•

Recursive definitions
Recursive algorithms
Recursive methods
Base cases
General cases

Java Programming: From Problem Analysis to Program Design, 5e

34
Chapter Summary (continued)
•
•
•
•
•

Tracing recursive methods
Designing recursive methods
Varieties of recursive methods
Recursion vs. iteration
Various recursive functions explored

Java Programming: From Problem Analysis to Program Design, 5e

35

Mais conteúdo relacionado

Mais procurados

Java string handling
Java string handlingJava string handling
Java string handlingSalman Khan
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in javaHitesh Kumar
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaMOHIT AGARWAL
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVASAGARDAVE29
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Abstraction java
Abstraction javaAbstraction java
Abstraction javaMahinImran
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in javaAhsan Raja
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Constructor in java
Constructor in javaConstructor in java
Constructor in javaHitesh Kumar
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in javaAtul Sehdev
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java pptkunal kishore
 
Java Data Types
Java Data TypesJava Data Types
Java Data TypesSpotle.ai
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and InterfaceHaris Bin Zahid
 

Mais procurados (20)

Java string handling
Java string handlingJava string handling
Java string handling
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Methods in java
Methods in javaMethods in java
Methods in java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Abstraction java
Abstraction javaAbstraction java
Abstraction java
 
Java threads
Java threadsJava threads
Java threads
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
 
Applets
AppletsApplets
Applets
 
File handling
File handlingFile handling
File handling
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 

Destaque

9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03Terry Yoast
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05Terry Yoast
 
Chapter 1 - An Overview of Computers and Programming Languages
Chapter 1 - An Overview of Computers and Programming LanguagesChapter 1 - An Overview of Computers and Programming Languages
Chapter 1 - An Overview of Computers and Programming LanguagesAdan Hubahib
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04Terry Yoast
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10Terry Yoast
 
9781111530532 ppt ch08
9781111530532 ppt ch089781111530532 ppt ch08
9781111530532 ppt ch08Terry Yoast
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07Terry Yoast
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaAdan Hubahib
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Traian Rebedea
 

Destaque (9)

9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05
 
Chapter 1 - An Overview of Computers and Programming Languages
Chapter 1 - An Overview of Computers and Programming LanguagesChapter 1 - An Overview of Computers and Programming Languages
Chapter 1 - An Overview of Computers and Programming Languages
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10
 
9781111530532 ppt ch08
9781111530532 ppt ch089781111530532 ppt ch08
9781111530532 ppt ch08
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3
 

Semelhante a Chapter 13 - Recursion

9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13Terry Yoast
 
9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15Terry Yoast
 
9781111530532 ppt ch02
9781111530532 ppt ch029781111530532 ppt ch02
9781111530532 ppt ch02Terry Yoast
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07Terry Yoast
 
9781439035665 ppt ch07
9781439035665 ppt ch079781439035665 ppt ch07
9781439035665 ppt ch07Terry Yoast
 
9781111530532 ppt ch02
9781111530532 ppt ch029781111530532 ppt ch02
9781111530532 ppt ch02Terry Yoast
 
9781439035665 ppt ch02
9781439035665 ppt ch029781439035665 ppt ch02
9781439035665 ppt ch02Terry Yoast
 
Introduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructureIntroduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructurePrasanna996462
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxsatvikkushwaha1
 
Types of Algorithms.ppt
Types of Algorithms.pptTypes of Algorithms.ppt
Types of Algorithms.pptALIZAIB KHAN
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03Terry Yoast
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06Terry Yoast
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.pptLokeshK66
 

Semelhante a Chapter 13 - Recursion (20)

9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13
 
Chapter 13
Chapter 13Chapter 13
Chapter 13
 
9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15
 
Chap14
Chap14Chap14
Chap14
 
9781111530532 ppt ch02
9781111530532 ppt ch029781111530532 ppt ch02
9781111530532 ppt ch02
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07
 
9781439035665 ppt ch07
9781439035665 ppt ch079781439035665 ppt ch07
9781439035665 ppt ch07
 
Ppt chapter12
Ppt chapter12Ppt chapter12
Ppt chapter12
 
9781111530532 ppt ch02
9781111530532 ppt ch029781111530532 ppt ch02
9781111530532 ppt ch02
 
9781439035665 ppt ch02
9781439035665 ppt ch029781439035665 ppt ch02
9781439035665 ppt ch02
 
Introduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructureIntroduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructure
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptx
 
Types of Algorithms.ppt
Types of Algorithms.pptTypes of Algorithms.ppt
Types of Algorithms.ppt
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
 
Cis068 08
Cis068 08Cis068 08
Cis068 08
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
 
RECURSION.pptx
RECURSION.pptxRECURSION.pptx
RECURSION.pptx
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
 
Knightstour
KnightstourKnightstour
Knightstour
 

Último

Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45MysoreMuleSoftMeetup
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the lifeNitinDeodare
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxheathfieldcps1
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Denish Jangid
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...Krashi Coaching
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17Celine George
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 

Último (20)

Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 

Chapter 13 - Recursion

  • 1. Java Programming: From Problem Analysis to Program Design, 5e Chapter 13 Recursion
  • 2. Chapter Objectives • Learn about recursive definitions • Explore the base case and the general case of a recursive definition • Learn about recursive algorithms Java Programming: From Problem Analysis to Program Design, 5e 2
  • 3. Chapter Objectives (continued) • Learn about recursive methods • Become aware of direct and indirect recursion • Explore how to use recursive methods to implement recursive algorithms Java Programming: From Problem Analysis to Program Design, 5e 3
  • 4. Recursive Definitions • Recursion – Process of solving a problem by reducing it to smaller versions of itself • Recursive definition – Definition in which a problem is expressed in terms of a smaller version of itself – Has one or more base cases Java Programming: From Problem Analysis to Program Design, 5e 4
  • 5. Recursive Definitions (continued) Java Programming: From Problem Analysis to Program Design, 5e 5
  • 6. Recursive Definitions (continued) • Recursive algorithm – Algorithm that finds the solution to a given problem by reducing the problem to smaller versions of itself – Has one or more base cases – Implemented using recursive methods • Recursive method – Method that calls itself • Base case – Case in recursive definition in which the solution is obtained directly – Stops the recursion Java Programming: From Problem Analysis to Program Design, 5e 6
  • 7. Recursive Definitions (continued) • General solution – Breaks problem into smaller versions of itself • General case – Case in recursive definition in which a smaller version of itself is called – Must eventually be reduced to a base case Java Programming: From Problem Analysis to Program Design, 5e 7
  • 8. Tracing a Recursive Method • Recursive method – Logically, you can think of a recursive method having unlimited copies of itself – Every recursive call has its own: • Code • Set of parameters • Set of local variables Java Programming: From Problem Analysis to Program Design, 5e 8
  • 9. Tracing a Recursive Method (continued) • After completing a recursive call – Control goes back to the calling environment – Recursive call must execute completely before control goes back to previous call – Execution in previous call begins from point immediately following recursive call Java Programming: From Problem Analysis to Program Design, 5e 9
  • 10. Recursive Definitions • Directly recursive: a method that calls itself • Indirectly recursive: a method that calls another method and eventually results in the original method call • Tail recursive method: recursive method in which the last statement executed is the recursive call • Infinite recursion: the case where every recursive call results in another recursive call Java Programming: From Problem Analysis to Program Design, 5e 10
  • 11. Designing Recursive Methods • Understand problem requirements • Determine limiting conditions • Identify base cases Java Programming: From Problem Analysis to Program Design, 5e 11
  • 12. Designing Recursive Methods (continued) • Provide direct solution to each base case • Identify general case(s) • Provide solutions to general cases in terms of smaller versions of general cases Java Programming: From Problem Analysis to Program Design, 5e 12
  • 13. Recursive Factorial Method public static int fact(int num) { if (num = = 0) return 1; else return num * fact(num – 1); } Java Programming: From Problem Analysis to Program Design, 5e 13
  • 14. Recursive Factorial Method (continued) Java Programming: From Problem Analysis to Program Design, 5e 14
  • 15. Largest Value in Array Java Programming: From Problem Analysis to Program Design, 5e 15
  • 16. Largest Value in Array (continued) • • if the size of the list is 1 the largest element in the list is the only element in the list else to find the largest element in list[a]...list[b] a. find the largest element in list[a + 1]...list[b] and call it max b. compare list[a] and max if (list[a] >= max) the largest element in list[a]...list[b] is list[a] else the largest element in list[a]...list[b] is max Java Programming: From Problem Analysis to Program Design, 5e 16
  • 17. Largest Value in Array (continued) public static int largest(int[] list, int lowerIndex, int upperIndex) { int max; if (lowerIndex == upperIndex) return list[lowerIndex]; else { max = largest(list, lowerIndex + 1, upperIndex); if (list[lowerIndex] >= max) return list[lowerIndex]; else return max; } } Java Programming: From Problem Analysis to Program Design, 5e 17
  • 18. Execution of largest (list, 0, 3) Java Programming: From Problem Analysis to Program Design, 5e 18
  • 19. Execution of largest (list, 0, 3) Java Programming: From Problem Analysis to Program Design, 5e 19
  • 20. Recursive Fibonacci Java Programming: From Problem Analysis to Program Design, 5e 20
  • 21. Recursive Fibonacci (continued) public static int rFibNum(int a, int b, int n) { if (n == 1) return a; else if (n == 2) return b; else return rFibNum(a, b, n -1) + rFibNum(a, b, n - 2); } Java Programming: From Problem Analysis to Program Design, 5e 21
  • 22. Recursive Fibonacci (continued) Java Programming: From Problem Analysis to Program Design, 5e 22
  • 23. Towers of Hanoi Problem with Three Disks Java Programming: From Problem Analysis to Program Design, 5e 23
  • 24. Towers of Hanoi: Three Disk Solution Java Programming: From Problem Analysis to Program Design, 5e 24
  • 25. Towers of Hanoi: Three Disk Solution (continued) Java Programming: From Problem Analysis to Program Design, 5e 25
  • 26. Towers of Hanoi: Recursive Algorithm public static void moveDisks(int count, int needle1, int needle3, int needle2) { if (count > 0) { moveDisks(count - 1, needle1, needle2, needle3); System.out.println("Move disk " + count + " from needle " + needle1 + " to needle " + needle3 + ". "); moveDisks(count - 1, needle2, needle3, needle1); } } Java Programming: From Problem Analysis to Program Design, 5e 26
  • 27. Recursion or Iteration? • Two ways to solve particular problem – Iteration – Recursion • Iterative control structures: use looping to repeat a set of statements • Tradeoffs between two options – Sometimes recursive solution is easier – Recursive solution is often slower Java Programming: From Problem Analysis to Program Design, 5e 27
  • 28. Programming Example: Decimal to Binary Java Programming: From Problem Analysis to Program Design, 5e 28
  • 29. Java Programming: From Problem Analysis to Program Design, 5e 29
  • 30. Sierpinski Gaskets of Various Orders Java Programming: From Problem Analysis to Program Design, 5e 30
  • 31. Programming Example: Sierpinski Gasket • Input: nonnegative integer indicating level of Sierpinski gasket • Output: triangle shape displaying a Sierpinski gasket of the given order • Solution includes: – Recursive method drawSierpinski – Method to find midpoint of two points Java Programming: From Problem Analysis to Program Design, 5e 31
  • 32. Programming Example: Sierpinski Gasket (continued) private void drawSierpinski(Graphics g, int lev, Point p1, Point p2, Point p3) { Point midP1P2; Point midP2P3; Point midP3P1; if (lev > 0) { g.drawLine(p1.x, p1.y, p2.x, p2.y); g.drawLine(p2.x, p2.y, p3.x, p3.y); g.drawLine(p3.x, p3.y, p1.x, p1.y); midP1P2 = midPoint(p1, p2); midP2P3 = midPoint(p2, p3); midP3P1 = midPoint(p3, p1); drawSierpinski(g, lev - 1, p1, midP1P2, midP3P1); drawSierpinski(g, lev - 1, p2, midP2P3, midP1P2); drawSierpinski(g, lev - 1, p3, midP3P1, midP2P3); } } Java Programming: From Problem Analysis to Program Design, 5e 32
  • 33. Programming Example: Sierpinski Gasket (continued) Java Programming: From Problem Analysis to Program Design, 5e 33
  • 34. Chapter Summary • • • • • Recursive definitions Recursive algorithms Recursive methods Base cases General cases Java Programming: From Problem Analysis to Program Design, 5e 34
  • 35. Chapter Summary (continued) • • • • • Tracing recursive methods Designing recursive methods Varieties of recursive methods Recursion vs. iteration Various recursive functions explored Java Programming: From Problem Analysis to Program Design, 5e 35