Computer Science 100

Computational Expression

Gregory M. Kapfhammer


flickr photo by wocintechchat.com shared under a CC (BY) 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 558 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!

Review the sum example on page 541

We have studied 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?

Recursively traversing a maze

Using the MazeSearch

Recursively traverse the maze

Perform the same operation on a redefined region

Review this program's source code

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)

Review the source code of this 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?

Focus on the intuition