Docsity

Recursion in Java Programming: Understanding Self-Referencing Methods and Structures, Study notes of Computer Science

Toggle sidebar
bg1
1
Introduction to Programming
with Java, for Beginners
Recursion
CIS110 1
Meaning of ā€œRecursiveā€
īš„ā€œRecursiveā€ means ā€œself referencingā€
īš„When a class has an instance variable whose type is
itself, we say it is ā€œrecursively definedā€. It’s an example of
ā€œstructural recursionā€.
īš„Notice that the class has subparts that look like the whole.
Person
public class Person{
private String name;
private Person mother;
private Person father;
}
CIS110 2
Recursive Methods
īš„A recursive method is one that calls itself
Common Pattern:
public type foo(parameters){
if (condition)
return value;
else return foo(arguments);
}
CIS110 3
Why Use Recursive Methods?
Many things we model have recursive structures
(where the subparts look like the whole):
īš„Mathematical formulas
•Power: x^y == x * x ^ (y-1)
•Factorial: n! == n * (n-1)!
•Fibonacci series: fib(n) == fib(n-1) + fib(n-2)
•Fractal geometry (Julia, Mandelbrot sets)
īš„Languages
•Programming languages:
•An expression can contain expressions
•A statement can contain other statements
•HTML tags in a web page
•XML (extensible markup language), for storing semi-structured data
īš„Hierarchical Entities (often stored as ā€œtreesā€)
•A ā€œfamily ancestral treeā€
•An operating system’s folder/directory hierarchy
īš„Graphs
•Social networks (Facebook)
•Computer networks
•Airline travel networks
Recursive methods are frequently used to process such structures
pf3

Partial preview of the text

Download Recursion in Java Programming: Understanding Self-Referencing Methods and Structures and more Study notes Computer Science in PDF only on Docsity!

Introduction to Programming

with Java, for Beginners

Recursion

CIS

1

Meaning of ā€œRecursiveā€

Ā„^

ā€œRecursiveā€ means ā€œself referencingā€ Ā„^

When a class has an instance variable whose type isitself, we say it is ā€œrecursively definedā€. It’s an example ofā€œstructural recursionā€. Ā„^

Notice that the class has subparts that look like the whole.

Person

public class Person{

private String name;private Person mother;private Person father; }

CIS

2

Recursive Methods

A recursive method is one that calls itself

Common Pattern:

public

type

foo(

parameters

if (

condition

return

value

else return foo(

arguments

CIS

3

Why Use Recursive Methods?

Many things we model have recursive structures(where the subparts look like the whole):

Ā„^

Mathematical formulas•

Power: x^y == x * x ^ (y-1)• Factorial: n! == n * (n-1)!• Fibonacci series: fib(n) == fib(n-1) + fib(n-2)• Fractal geometry (Julia, Mandelbrot sets) Ā„^

Languages•

Programming languages:

-^

An expression can contain expressions

-^

A statement can contain other statements

-^

HTML tags in a web page

-^

XML (extensible markup language), for storing semi-structured data

Ā„^

Hierarchical Entities (often stored as ā€œtreesā€)•

A ā€œfamily ancestral treeā€ā€¢ An operating system’s folder/directory hierarchy Ā„^

Graphs•

Social networks (Facebook)• Computer networks• Airline travel networks Recursive methods are frequently used to process such structures

CIS

4

Write a Recursive Method

ƒ^

Rewrite it as a recursive method^ ƒ

Instead of having a loop, have it call itself

ƒ^

How do you get it to stop when it should?

ƒ^

Can we generalize the steps we took as a way toapproach the coding of recursive methods?

ƒ^

Consider this method:

public static void countDown(int num){

if (num <= 0)

return; for (int i = num; i >= 1; i--){

System.out.println(num); } }

CIS

5

Example: print down to 1

/ Recursive method that prints**

num, num-1, num-2, ... 1.

*** Assumes num is positive./public static void printDownTo1(int num){*

if (num == 0) // base case

return; System.out.println("num is " + num);printDownTo1(num - 1);

}

CIS

6

Recursion Recipe1)^

Handle the ā€œbase caseā€, which is when a recursive callshould

not

be made because the process should stop.

For other cases, make a recursive call (the recursiveā€œleap of faithā€), which

must make progress towards a

goal

(towards the base case).

If the base case is not handled, or the recursive call doesnot progress towards the base case, then the method islikely to call itself infinitely (it will ā€œdivergeā€), and you’ll get aā€œstack overrunā€ error.

CIS

7

Example: sum numbers /*

A

recursive

method

that

returns

the

sum

of

the

numbers

from

start

to

end

public static int sum(int start, int end){

if (start > end) // base case

return 0; return start + sum(start + 1, end); }