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.

proportional to n, however, s<strong>in</strong>ce each box is visited <strong>in</strong> constant time when stepp<strong>in</strong>g<br />

through our algorithm <strong>and</strong> there are 2n − 1 boxes.<br />

Figure 3.25: Recursion trace for the execution of<br />

B<strong>in</strong>arySum(0,8).<br />

Comput<strong>in</strong>g Fibonacci Numbers via B<strong>in</strong>ary Recursion<br />

Let us consider the problem of comput<strong>in</strong>g the kth Fibonacci number. Recall from<br />

Section 2.2.3, that the Fibonacci numbers are recursively def<strong>in</strong>ed as follows:<br />

F 0 = 0<br />

F 1 = 1<br />

F i = F i−1 + F i−2 for i < 1.<br />

By directly apply<strong>in</strong>g this def<strong>in</strong>ition, Algorithm B<strong>in</strong>aryFib, shown <strong>in</strong> Code<br />

Fragment 3.35, computes the sequence of Fibonacci numbers us<strong>in</strong>g b<strong>in</strong>ary<br />

recursion.<br />

Code Fragment 3.35: Comput<strong>in</strong>g the kth Fibonacci<br />

number us<strong>in</strong>g b<strong>in</strong>ary recursion.<br />

200

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

Saved successfully!

Ooh no, something went wrong!