Recursion is not a simple "self-called yourself", nor a simple "interactive call ". It is a method and idea for analyzing and solving problems. To put it simply, recursive thinking is to break down the problem into smaller ones with the same solution as the original one. For example, the binary search algorithm constantly scales down the problem (half of the original problem), and the new problem has the same solution as the original problem.
Many people do not have a deep understanding of recursion. It remains to the extent that "you call yourself. This is actually just a recursive representation (strictly speaking, the representation is not comprehensive, because in addition to the recursion of "self-called yourself", there is also the recursion of interactive calls ). Recursive thinking is far more simple than that.
Recursion is not a simple "self-called yourself", nor a simple "interactive call ". It is a method and idea for analyzing and solving problems. To put it simply, recursive thinking is to break down the problem into smaller ones with the same solution as the original one. For example, the binary search algorithm constantly scales down the problem (half of the original problem), and the new problem has the same solution as the original problem.
Some problems are hard to solve or even have no solution by using traditional iterative algorithms, but recursion can easily solve them. For example, the Tower of Hanoi issue. However, recursive use also has its disadvantages because it requires multi-layer function calls, which consumes a lot of stack space and function call time.
Since the recursive idea is to break down the problem into a smaller scale and have the same solution as the original problem, can this problem be solved by recursion? The answer is No. Not all problems can be solved using recursion. So what problems can be solved using recursion? Generally, the problems solved by recursion must meet two conditions:
- Recursive calling can be used to narrow down the problem scale, and the new problem is in the same form as the original problem.
- There is a simple situation that enables recursion to exit in a simple situation.
If a problem does not meet the above two conditions, it cannot be solved by recursion.
For ease of understanding, take the Fibonacci series as an example: calculate the nth value of the Fibonacci series.
This is a classic problem. this problem must be mentioned when it comes to recursion. The Fibonacci series is defined as follows: f (0) = 0, f (1) = 1, for n> 1, f (n) = f (n-1) + f (n-2)
This is an obvious problem that can be solved using recursion. Let's take a look at how it meets two recursive conditions:
- For a n> 2, f (n) only needs to find f (n-1) and f (n-2), that is to say, the problem of scale n is transformed into a smaller problem;
- For n = 0 and n = 1, there is a simple situation: f (0) = 0, f (1) = 1.
Therefore, we can easily write the recursive program for calculating the nth entry of the Fibonacci series:
int fib(n){ if(n == 0) return 0; else if(n == 1) return 1; else return f(n-1) + f(n-2);}
When writing a function called by recursion, you must write the judgment on a simple situation at the beginning to ensure that the function call can stop recursion in time when detecting a simple situation. otherwise, your function may be called recursively without stopping.
Additional reading
The topic list of this article is as follows:
- Recursive: recursive thinking
- Recursion: two conditions that must be met by recursion
- Recursion: recursive determination of string-to-text phenomena
- Recursive: recursive implementation of binary search algorithms
- Recursion: the efficiency of recursion
- Recursion: recursion and loop
- Let's talk about recursion: Is loop and iteration the same thing?
- Recursive computing and iterative computing
- On Recursion: Learn about tail recursion from Fibonacci
- Recursive: Tail recursion and CPS
- Recursion: add more knowledge about Continuation.
- Recursion: Tail recursion in PHP and its optimization
- Recursion: Tail recursion optimization from the perspective of Assembly
This article is available at http://www.nowamagic.net/librarys/veda/detail/2315.