SUNY Geneseo Department of Computer Science


Introduction to Recursion

{Date}

CSci 141, Fall 2004
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

Hand out Lab 3

No Friday office hours this week

Questions?

Using robot with Sun Java tools?

[Subfolders for Parts of "geneseo.cs.sc" Package]

Averaging measurements for lab 2?

Reading Summary

Sections 6.1 - 6.3

(Lots of this summary got lost when my computer crashed after running its battery down)

Recursion is solving a problem in terms of smaller versions of itself

Value-producing recursion must always return a value

Value-producing recursions always have at least 2 return statements

Three kinds of recursion

Why Care about Recursion?

Towers of Hanoi

Goal: Move the tower from the left pole to the right (or vice versa)

Rules:

Recursion Examples

Imagine a "Super Array" class that represents arrays with some additional features

Arrays

[An Array of Characters]

    class SuperArray {

        private int[] A;      // Underlying array
        private int n;        // Size of array

        public SuperArray( int size ) {
            A = new int[ size ];
            n = size;
        }

        public getElement( int i ) {
            return A[i];
        }

        ...

Design a recursive method for SuperArray that initializes elements 0 through i to 0

    init( int i )
        if i < 0          // test
            do nothing    // base case
        else
            A[i] = 0;     // Recursive case
            init( i - 1 )

Next

Some more sophisticated recursions

Read Sections 6.4 and 6.5


Next Lecture