91
Chapter 17
Recursion
Chapter 17
RECURSION
CHAPTER GOALS
•
To be able to identify the base case(s) and the general case in a recursive definition.
•
To be able to write a recursive algorithm for a problem involving only simple variables.
•
To be able to write a recursive algorithm for a problem involving structured variables.
•
To be able to write a recursive algorithm for a problem involving linked lists.
CHAPTER OUTLINE
I.
What Is Recursion?
II.
Recursive Algorithms with Simple Variables
III.
Towers of Hanoi
IV.
Recursive Algorithms with Structured Variables
V.
Recursion Using Pointer Variables
A.
Printing a Dynamic Linked List in Reverse Order
B.
Copying a Dynamic Linked List
VI.
Recursion or Iteration?
VII.
Problem-Solving Case Study
: Converting Decimal Integers to Binary Integers
VIII.
Problem-Solving Case Study
: Minimum Value in an Integer Array
IX. Testing and Debugging
A.
Testing and Debugging Hints
X. Summary
GENERAL DISCUSSION
This chapter introduces recursion not only as a mechanism for control flow within a program but also as a
means of designing solutions to problems.
A key concept is that some problems can be solved more easily with a recursive algorithm than with an
iterative solution. Students must understand recursive definitions in order to recognize such problems.
As part of learning about recursive definitions, it's important for students to realize that the base case may
be empty—that is, it may require no action except termination of the recursion. Another important idea is
that the recursive call may come at any point in the algorithm: at the beginning, at the end, or in the middle.
There may even be multiple recursive calls.
Although most students experience difficulty in creating recursive solutions, there are some people who
naturally think in a recursive fashion. There is a story about a small boy who was asked in class to define
infinity. He replied that it was like the picture on the Cream of Wheat box. After being told that he was
wrong and that infinity meant "unbounded in time or space," he explained that this was what was shown on
the Cream of Wheat box: "There's a picture of a man holding a box of Cream of Wheat, and on that box
there's another picture of a man holding a box of Cream of Wheat, and so on for ever and ever." This boy's
concept of infinity is an example of infinite recursion. Should you encounter students who understand
recursion easily, you might encourage them to learn LlSP, a recursion-oriented language used in artificial
intelligence research.
The most important point for students to learn about recursion is that there are some cases in which it is
not appropriate. Some problems do not have graceful recursive solutions. And other problems that have
This
preview
has intentionally blurred sections.
Sign up to view the full version.
92
Chapter 17
Recursion
recursive solutions can be solved more efficiently with iterative solutions.

This is the end of the preview.
Sign up
to
access the rest of the document.
- Spring '04
- Lebre
- Computer Science, Recursion, Control flow
-
Click to edit the document details