Java Reference
In-Depth Information
x - and y -coordinates. Invoking p1.midpoint(p2) returns a new Point2D object that is the
midpoint between p1 and p2 (lines 81-83).
18.23 How do you obtain the midpoint between two points?
18.24
Check
Point
What is the base case for the displayTriangles method?
18.25
How many times is the displayTriangles method invoked for a Sierpinski trian-
gle of order 0, order 1, order 2, and order n?
18.26
What happens if you enter a negative order? How do you fix this problem in the code?
18.27
Instead of drawing a triangle using a polygon, rewrite the code to draw a triangle by
drawing three lines to connect the points in lines 71-77.
18.9 Recursion vs. Iteration
Recursion is an alternative form of program control. It is essentially repetition without
a loop.
Key
Point
When you use loops, you specify a loop body. The repetition of the loop body is controlled
by the loop control structure. In recursion, the method itself is called repeatedly. A selection
statement must be used to control whether to call the method recursively or not.
Recursion bears substantial overhead. Each time the program calls a method, the system
must allocate memory for all of the method's local variables and parameters. This can con-
sume considerable memory and requires extra time to manage the memory.
Any problem that can be solved recursively can be solved nonrecursively with iterations.
Recursion has some negative aspects: it uses up too much time and too much memory. Why,
then, should you use it? In some cases, using recursion enables you to specify a clear, sim-
ple solution for an inherently recursive problem that would otherwise be difficult to obtain.
Examples are the directory-size problem, the Tower of Hanoi problem, and the fractal prob-
lem, which are rather difficult to solve without using recursion.
The decision whether to use recursion or iteration should be based on the nature of, and
your understanding of, the problem you are trying to solve. The rule of thumb is to use which-
ever approach can best develop an intuitive solution that naturally mirrors the problem. If an
iterative solution is obvious, use it. It will generally be more efficient than the recursive option.
recursion overhead
recursion advantages
recursion or iteration?
Note
Recursive programs can run out of memory, causing a StackOverflowError .
StackOverflowError
Tip
If you are concerned about your program's performance, avoid using recursion, because
it takes more time and consumes more memory than iteration. In general, recursion
can be used to solve the inherent recursive problems such as Tower of Hanoi, recursive
directories, and Sierpinski triangles.
performance concern
18.28 Which of the following statements are true?
a. Any recursive method can be converted into a nonrecursive method.
Check
Point
b. Recursive methods take more time and memory to execute than nonrecursive methods.
c. Recursive methods are always simpler than nonrecursive methods.
d. There is always a selection statement in a recursive method to check whether a
base case is reached.
18.29
What is a cause for a stack-overflow exception?
 
 
 
Search WWH ::




Custom Search