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.

The factorial function can be def<strong>in</strong>ed <strong>in</strong> a manner that suggests a recursive<br />

formulation. To see this, observe that<br />

factorial(5) = 5 · (4 · 3 · 2 · 1) = 5 · factorial(4).<br />

Thus, we can def<strong>in</strong>e factorial(5) <strong>in</strong> terms of factorial(4). In general, for a<br />

positive <strong>in</strong>teger n, we can def<strong>in</strong>e factorial(n) to be n·factorial(n − 1). This<br />

leads to the follow<strong>in</strong>g recursive def<strong>in</strong>ition.<br />

This def<strong>in</strong>ition is typical of many recursive def<strong>in</strong>itions. First, it conta<strong>in</strong>s one or<br />

more base cases, which are def<strong>in</strong>ed nonrecursively <strong>in</strong> terms of fixed quantities. In<br />

this case, n = 0 is the base case. It also conta<strong>in</strong>s one or more recursive cases, which<br />

are def<strong>in</strong>ed by appeal<strong>in</strong>g to the def<strong>in</strong>ition of the function be<strong>in</strong>g def<strong>in</strong>ed. Observe<br />

that there is no circularity <strong>in</strong> this def<strong>in</strong>ition, because each time the function is<br />

<strong>in</strong>voked, its argument is smaller by one.<br />

A Recursive Implementation of the Factorial Function<br />

Let us consider a <strong>Java</strong> implementation of the factorial function shown <strong>in</strong> Code<br />

Fragment 3.29 under the name recursiveFactorial(). Notice that no<br />

loop<strong>in</strong>g was needed here. The repeated recursive <strong>in</strong>vocations of the function takes<br />

the place of loop<strong>in</strong>g.<br />

Code Fragment 3.29:<br />

the factorial function.<br />

A recursive implementation of<br />

We can illustrate the execution of a recursive function def<strong>in</strong>ition by means of a<br />

recursion trace. Each entry of the trace corresponds to a recursive call. Each new<br />

recursive function call is <strong>in</strong>dicated by an arrow to the newly called function. When<br />

the function returns, an arrow show<strong>in</strong>g this return is drawn <strong>and</strong> the return value may<br />

be <strong>in</strong>dicated with this arrow. An example of a trace is shown <strong>in</strong> Figure 3.21.<br />

What is the advantage of us<strong>in</strong>g recursion? Although the recursive implementation<br />

of the factorial function is somewhat simpler than the iterative version, <strong>in</strong> this case<br />

there is no compell<strong>in</strong>g reason for preferr<strong>in</strong>g recursion over iteration. For some<br />

189

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

Saved successfully!

Ooh no, something went wrong!