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.

In Section 7.3.6 we described how to evaluate an arithmetic expression us<strong>in</strong>g a<br />

postorder traversal, which is exactly the algorithm the JVM uses. We described<br />

that algorithm <strong>in</strong> a recursive way, however, not <strong>in</strong> a way that explicitly uses an<br />

oper<strong>and</strong> stack. Nevertheless, this recursive description is equivalent to a<br />

nonrecursive version based on us<strong>in</strong>g an oper<strong>and</strong> stack.<br />

Implement<strong>in</strong>g Recursion<br />

One of the benefits of us<strong>in</strong>g a stack to implement method <strong>in</strong>vocation is that it<br />

allows programs to use recursion. That is, it allows a method to call itself, as<br />

discussed <strong>in</strong> Section 3.5. Interest<strong>in</strong>gly, early programm<strong>in</strong>g languages, such as<br />

Cobol <strong>and</strong> Fortran, did not orig<strong>in</strong>ally use run-time stacks to implement method<br />

<strong>and</strong> procedure calls. But because of the elegance <strong>and</strong> efficiency that recursion<br />

allows, all modern programm<strong>in</strong>g languages, <strong>in</strong>clud<strong>in</strong>g the modern versions of<br />

classic languages like Cobol <strong>and</strong> Fortran, utilize a run-time stack for method <strong>and</strong><br />

procedure calls.<br />

In the execution of a recursive method, each box of the recursion trace<br />

corresponds to a frame of the <strong>Java</strong> method stack. Also, the content of the <strong>Java</strong><br />

method stack corresponds to the cha<strong>in</strong> of boxes from the <strong>in</strong>itial method <strong>in</strong>vocation<br />

to the current one.<br />

To better illustrate how a run-time stack allows for recursive methods, let us<br />

consider a <strong>Java</strong> implementation of the classic recursive def<strong>in</strong>ition of the factorial<br />

function,<br />

n! = n(n − 1)(n − 2)1,<br />

as shown <strong>in</strong> Code Fragment 14.1.<br />

Code Fragment 14.1: Recursive method<br />

factorial.<br />

The first time we call method factorial, its stack frame <strong>in</strong>cludes a local<br />

variable stor<strong>in</strong>g the value n. Method factorial() recursively calls itself to<br />

compute (n − 1)!, which pushes a new frame on the <strong>Java</strong> run-time stack. In turn,<br />

this recursive <strong>in</strong>vocation calls itself to compute (n − 2)!, etc. The cha<strong>in</strong> of<br />

889

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

Saved successfully!

Ooh no, something went wrong!