12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

130 Chap. 4 Lists, Stacks, <strong>and</strong> Queuester 1 is made, requiring that the stack s<strong>to</strong>re the calling address (say β 3 ) <strong>and</strong> currentvalue (which is 2).At this point, we have reached the base case for fact, <strong>and</strong> so the recursionbegins <strong>to</strong> unwind. Each return from fact involves popping the s<strong>to</strong>red value forn from the stack, along with the return address from the function call. The returnvalue for fact is multiplied by the res<strong>to</strong>red value for n, <strong>and</strong> the result is returned.Because an activation record must be created <strong>and</strong> placed on<strong>to</strong> the stack foreach subroutine call, making subroutine calls is a relatively expensive operation.While recursion is often used <strong>to</strong> make implementation easy <strong>and</strong> clear, sometimesyou might want <strong>to</strong> eliminate the overhead imposed by the recursive function calls.In some cases, such as the fac<strong>to</strong>rial function of Section 2.5, recursion can easily bereplaced by iteration.Example 4.2 As a simple example of replacing recursion with a stack,consider the following non-recursive version of the fac<strong>to</strong>rial function.static long fact(int n) { // Compute n!// To fit n! in a long variable, require n < 21assert (n >= 0) && (n 1) S.push(n--);long result = 1;while (S.length() > 0)result = result * S.pop();return result;}Here, we simply push successively smaller values of n on<strong>to</strong> the stack untilthe base case is reached, then repeatedly pop off the s<strong>to</strong>red values <strong>and</strong>multiply them in<strong>to</strong> the result.In practice, an iterative form of the fac<strong>to</strong>rial function would be both simpler<strong>and</strong> faster than the version shown in Example 4.2. Unfortunately, it is not alwayspossible <strong>to</strong> replace recursion with iteration. Recursion, or some imitation of it, isnecessary when implementing algorithms that require multiple branching such asin the Towers of Hanoi algorithm, or when traversing a binary tree. The Mergesort<strong>and</strong> Quicksort algorithms of Chapter 7 are also examples in which recursion isrequired. Fortunately, it is always possible <strong>to</strong> imitate recursion with a stack. Let usnow turn <strong>to</strong> a non-recursive version of the Towers of Hanoi function, which cannotbe done iteratively.

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

Saved successfully!

Ooh no, something went wrong!