11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Sec. 4.2 Stacks 123you might want to elimin<strong>at</strong>e the overhead imposed by the recursive function calls.In some cases, such as the factorial function of Section 2.5, recursion can easily bereplaced by iter<strong>at</strong>ion.Example 4.2 As a simple example of replacing recursion with a stack,consider the following non-recursive version of the factorial function./** @return n! */st<strong>at</strong>ic long fact(int 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 onto the stack untilthe base case is reached, then repe<strong>at</strong>edly pop off the stored values <strong>and</strong>multiply them into the result.An iter<strong>at</strong>ive form of the factorial function is both simpler <strong>and</strong> faster than theversion shown in Example 4.2. But it is not always possible to replace recursionwith iter<strong>at</strong>ion. Recursion, or some imit<strong>at</strong>ion of it, is necessary when implementingalgorithms th<strong>at</strong> require multiple branching such as in the Towers of Hanoi algorithm,or when traversing a binary tree. The Mergesort <strong>and</strong> Quicksort algorithmsof Chapter 7 are also examples in which recursion is required. Fortun<strong>at</strong>ely, it is alwayspossible to imit<strong>at</strong>e recursion with a stack. Let us now turn to a non-recursiveversion of the Towers of Hanoi function, which cannot be done iter<strong>at</strong>ively.Example 4.3 The TOH function shown in Figure 2.2 makes two recursivecalls: one to move n − 1 rings off the bottom ring, <strong>and</strong> another to movethese n − 1 rings back to the goal pole. We can elimin<strong>at</strong>e the recursion byusing a stack to store a represent<strong>at</strong>ion of the three oper<strong>at</strong>ions th<strong>at</strong> TOH mustperform: two recursive calls <strong>and</strong> a move oper<strong>at</strong>ion. To do so, we must firstcome up with a represent<strong>at</strong>ion of the various oper<strong>at</strong>ions, implemented as aclass whose objects will be stored on the stack.Figure 4.23 shows such a class. We first define an enumer<strong>at</strong>ed typecalled TOHop, with two values MOVE <strong>and</strong> TOH, to indic<strong>at</strong>e calls to themove function <strong>and</strong> recursive calls to TOH, respectively. Class TOHobjstores five values: an oper<strong>at</strong>ion field (indic<strong>at</strong>ing either a move or a newTOH oper<strong>at</strong>ion), the number of rings, <strong>and</strong> the three poles. Note th<strong>at</strong> the

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

Saved successfully!

Ooh no, something went wrong!