






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: Intro to Computer Science II; Subject: Computer Science; University: University of San Francisco (CA); Term: Unknown 1989;
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!
Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science — University of San Francisco – p.1/
Recursion is a fundamental problem-solving technique Involves decomposing a problem into: A base case that can be solved directly A recursive step that indicates how to handle morecomplex cases. A common recursive example is factorial: long factorial(int input) if (input ==
return 1; else return input
factorial(input
1); Department of Computer Science — University of San Francisco – p.2/
A common error in recursion is forgetting the base case. This can lead to infinite recursion double factorial(int input) return input
factorial(input
1); This eill eventually have a stack overflow. Department of Computer Science — University of San Francisco – p.4/
The Fibonacci numbers are defined as follows: f
f
f
n
f
n
f
n
The first few numbers are 1,1,2,3,5,8,13,21,... Write a class called Fibonacci. It should have a methodcalled getFib(int n) that calculates the nth Fibonaccinumber, plus a main method to test it. Department of Computer Science — University of San Francisco – p.5/
Solving a maze is the sort of problem that requirestrial-and-error. When you’re stuck, back up and undo the last thing youdid. This sort of approach works well with recursion. We’ll represent the maze as a two-dimensional array. 1 = clear, 0 = blocked. Start in the upper left, get to the lower right. Department of Computer Science — University of San Francisco – p.7/
Change the rules so that you always try to go left, thenright, then up, then down. Write a Maze constructor that takes two arguments: rowand col and generates a random maze of that size. Department of Computer Science — University of San Francisco – p.8/
Add your own pictures to the applet. Change the applet so that the recursive part of the pictureis in the lower right. Department of Computer Science — University of San Francisco – p.10/
We can also use recursion to draw fractals Example: Koch snowflake Rule: Each line segment is replaced by a “wedge” withsides that are the same length as the replaced piece. As we increase the depth, it begins to look like asnowflake. Department of Computer Science — University of San Francisco – p.11/