23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

• A s<strong>in</strong>gle tick of length L<br />

• A <strong>in</strong>terval with a central tick length L − 1.<br />

With each recursive call, the length decreases by one. When the length drops to<br />

zero, we simply return. As a result, this recursive process will always term<strong>in</strong>ate.<br />

This suggests a recursive process, <strong>in</strong> which the first <strong>and</strong> last steps are performed by<br />

call<strong>in</strong>g the drawTicks(L − 1) recursively. The middle step is performed by<br />

call<strong>in</strong>g the function drawOneTick(L). This recursive formulation is shown <strong>in</strong><br />

Code Fragment 3.30. As <strong>in</strong> the factorial example, the code has a base case (when L<br />

= 0). In this <strong>in</strong>stance we make two recursive calls to the function.<br />

Code Fragment 3.30: A recursive implementation of<br />

a function that draws a ruler.<br />

Illustrat<strong>in</strong>g Ruler Draw<strong>in</strong>g us<strong>in</strong>g a Recursion Trace<br />

The recursive execution of the recursive drawTicks function, def<strong>in</strong>ed above, can<br />

be visualized us<strong>in</strong>g a recursion trace.<br />

192

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!