递归可以帮助我们用比较直观的方式解决一些问题,如汉诺塔、科赫曲线等。
Summary Section 18.1 Introduction • A recursive method calls itself directly or indirectly through another method. Section 18.2 Recursion Concepts • When a recursive method is called to solve a problem, it can solve only the simplest case(s), or base case(s). If called with a base case, the method returns a result. • If a recursive method is called with a more complex problem than a base case, it typically divides the problem into two conceptual pieces — a piece that the method knows how to do and a piece that it does not know how to do. • To make recursion feasible, the piece that the method does not know how to do must resemble the original problem, but be a slightly simpler or smaller version of it. Because this new problem resembles the original problem, the method calls a fresh copy of itself to work on the smaller problem—this is called the recursion step. • For recursion to eventually terminate, each time a method calls itself with a simpler version of the original problem, the sequence of smaller and smaller problems must converge on a base case. When, the method recognizes the base case, it returns a result to the previous copy of the method. • A recursive call may be a call to another method, which in turn makes a call back to the original method. Such a process still results in a recursive call to the original method. This is known as an indirect recursive call or indirect recursion. Section 18.3 Example Using Recursion: Factorials • Either omitting the base case or writing the recursion step incorrectly so that it does not converge on the base case can cause infinite recursion, eventually exhausting memory. This error is analogous to the problem of an infinite loop in an iterative (nonrecursive) solution. Section 18.5 Example Using Recursion: Fibonacci Series • The Fibonacci series begins with 0 and 1 and has the property that each subsequent Fibonacci number is the sum of the preceding two. • The ratio of successive Fibonacci numbers converges on a constant value of 1.618…, a number that has been called the golden ratio or the golden mean. • Some recursive solutions, such as Fibonacci, result in an “explosion” of method calls. Section 18.6 Recursion and the Method-Call Stack • The executing method is always the one whose stack frame is at the top of the stack, and the stack frame for that method contains the values of its local variables. Section 18.7 Recursion vs. Iteration • Both iteration and recursion are based on a control statement: Iteration uses a repetition statement, recursion a selection statement. • Both iteration and recursion involve repetition: Iteration explicitly uses a repetition statement, whereas recursion achieves repetition through repeated method calls. • Iteration and recursion each involve a termination test: Iteration terminates when the loop-continuation condition fails, recursion when a base case is recognized. • Iteration with counter-controlled repetition and recursion each gradually approach termination: Iteration keeps modifying a counter until the counter assumes a value that makes the loop-continuation condition fail, whereas recursion keeps producing simpler versions of the original problem until the base case is reached. • Both iteration and recursion can occur infinitely: An infinite loop occurs with iteration if the loop-continuation test never becomes false, whereas infinite recursion occurs if the recursion step does not reduce the problem each time in a manner that converges on the base case. • Recursion repeatedly invokes the mechanism, and consequently the overhead, of method calls. • Any problem that can be solved recursively can also be solved iteratively. • A recursive approach is normally preferred over an iterative approach when it more naturally mirrors the problem and results in a program that is easier to understand and debug. • A recursive approach can often be implemented with few lines of code, but a corresponding iterative approach might take a large amount of code. Another reason to choose a recursive solution is that an iterative solution might not be apparent. Section 18.9 Fractals • A fractal is a geometric figure that is generated from a pattern repeated recursively an infinite number of times. • Fractals have a self-similar property—subparts are reduced-size copies of the whole. Section 18.10 Recursive Backtracking • In recursive backtracking, if one set of recursive calls does not result in a solution to the problem, the program backs up to the previous decision point and makes a different decision, often resulting in another set of recursive calls. |