Computer Science 111:

Introduction to Computer Science I

Gregory M. Kapfhammer


flickr photo shared by Yelp.com under a Creative Commons ( BY-NC-ND ) license

Color Scheme

Key Concept

Corresponding Diagram

In-Class Discussion

In-Class Activity

Details in the Textbook

Recursive programming

Recursion achieves repetition

Self similar behavior

Look at the Mandelbrot fractals

Look at page 603 for the applet's output

Elegant (yet, sometimes confusing!) approach

Why not use iteration?

Program should match the problem!

How? New parameters and local variables

Let's develop a basic understanding!

Program the sum example on page 587

We have finished a full program!

Recursive Examples

Recursive sum

Can you find the recursive call?

Can you find the base case?

How does these programs work?

Recursion

Method makes one or more calls to itself

Process stops when a base case is reached

Visualized with a call tree or a program stack

Data structure relies on smaller instances of itself

Any questions about the definitions?

Recursive Examples

Recursive factorial and recursive GCD

Compile and run these programs

Can you find the recursive call?

Can you find the base case?

How do these programs work?

Review recursive factorial in greater detail

Review recursive GCD in greater detail

Recursively traversing a maze

Using the MazeSearch

Recursively traverse the maze

Perform the same operation on a redefined region

Compile and run this program

Make sure to use the MazeSearch program

Can you find the recursive calls?

Can you find the base case?

How does this program work?

See Section 12.3 for more details

Solving the Hanoi Puzzle

Recursively solve the puzzle

Perform the same operation on different disk(s)

Compile and run this program

Make sure to use the SolveTowers program

Can you find the recursive calls?

Can you find the base case?

How does this program work?

See Section 12.3 for more details

Recursion versus iteration

How do we pick the right one?

Any questions about recursion?