Many people who do not understand recursion (today I saw a message from a beginner in csdn) always think that recursion is completely unnecessary and can be implemented through loops. In fact, this is a superficial understanding. The reason why recursion is popular in programs is not because of its cycle. we all know that recursion is divided into two steps, namely, delivery and conversion, so we can know that recursion is just a curse for space performance, this is simply unacceptable for those pursuing perfect time and space. If recursion is just a loop, we can estimate why recursion is used.
In programming, it is estimated that the most confusing basic algorithm is recursion. Many times, it takes a little time to understand a complex recursion, especially when the concept of the model is unclear, it is even more difficult to design a recursion by yourself.
Many people who do not understand recursion (today I saw a message from a beginner in csdn) always think that recursion is completely unnecessary and can be implemented through loops. In fact, this is a superficial understanding. The reason why recursion is popular in programs is not because of its cycle. we all know that recursion is divided into two steps, namely, delivery and conversion, so we can know that recursion is just a curse for space performance, this is simply unacceptable for those pursuing perfect time and space. If recursion is just a loop, we cannot see recursion now. Recursion still exists because recursion can generate an infinite loop body, that is, it may generate a 100-layer or 10000-layer for loop. For example, if you want to arrange a string in full and the length of the string is not fixed, if you use loops, you will find that you cannot write it at all. in this case, you need to call recursion, in addition, branch recursion can also be used in the recursive model, such as for loop and recursive nesting, or this section enumerates several recursive step expressions, each of which forms a recursion.
Use induction to understand recursion
The first reaction is what the recursive mathematical model is. After all, we have more mathematical modeling of problems than code modeling. (Of course, if you are clear about the problem, you can directly use the resume recursive model. using digital models as an intermediary is intended for those who are not clear about the problem)
By observing recursion, we will find that the recursive mathematical model is actually the induction method, which is the most common in the high-column series. Recall the induction method.
Induction is suitable for converting a problem into a sub-problem, and his sub-problem becomes a sub-problem, and we find that these problems are actually a model, that is to say, the same logic inductive processing item exists. Of course, there is an exception, that is, the processing method of recursive termination is not applicable to our inductive processing items, and of course it cannot be applied, otherwise we will be infinitely recursive. Here, an inductive endpoint and an expression for direct solution are introduced. If the list is used to describe the induction:
- Step expression: the expression for converting a problem into a subproblem.
- End condition: When can I stop using a stepping expression?
- Directly solving expressions: expressions that can directly calculate the return value under the ending condition
- Logical induction: applicable to the handling of all subproblems that are not applicable to the ending condition. of course, the preceding step expression is actually included here.
This is actually the end, and recursion will come out. The general form of recursive algorithms:
Void func (mode) {if (endCondition) {constExpression // basic item} else {accumrateExpreesion // induction entry mode = expression // Step-by-step expression func (mode) // call itself, recursion }}
The most typical is N! Algorithm, which is the most convincing. After understanding the concept of recursion and the use scenario, you can basically design it by yourself. of course, you need to combine it with other algorithms and continue to practice and summarize it.
# Include "stdio. h "# include" math. h "int main (void) {int n, rs; printf (" Enter the number of factorial n: "); scanf (" % d ", & n ); rs = factorial (n); printf ("% d", rs);} // recursive calculation process int factorial (n) {if (n = 1) {return 1;} return n * factorial (n-1 );}
The recursion of factorial is relatively simple, so it will not be expanded here.
Two more recursive examples
Returns the depth of a binary tree:
int depth(Tree t){ if(!t) return 0; else { int a=depth(t.right); int b=depth(t.left); return (a>b)?(a+1):(b+1); } }
Determine whether a binary tree is balanced:
int isB(Tree t){ if(!t) return 0; int left=isB(t.left); int right=isB(t.right); if( left >=0 && right >=0 && left - right <= 1 || left -right >=-1) return (left < right)? (right +1) : (left + 1); else return -1; }
The first algorithm is better understood, but the second algorithm is less understandable. The idea of the first algorithm is: if this tree is empty, 0 is returned; otherwise, the depth of the left tree is obtained first, and the depth of the right number is obtained, then, compare the two values and take the value + 1 if the value is greater. The second algorithm should first understand the functions of the isB function. it returns 0 for the empty tree and-1 for the depth of the returned tree. If you understand the functions of a function, you can see more in the code. if a function returns-1, the entire function returns-1. (You only need to carefully check the process)
The best way to understand recursion is to understand the functional meaning of a function. Understand how a problem is broken down into its subproblems, so that you can understand the recursive function code. There is a misunderstanding here (I have been deeply immersed in it), that is, by analyzing the stack, analyzing the call process and output results of a function to analyze recursive algorithms. This is very important. in this way, we will only get dizzy. actually, recursion is essentially a function call. actually, there is no difference between the called function and itself. When a function is called, some temporary information is always saved to the stack, so that the function can return the correct information. We only need to know that recursion will lead to a large number of function calls and a large number of stack operations.
Summary
The basic idea of recursion is to convert the problems of large scale into similar subproblems of small scale. In function implementation, because the method to solve the big problem and the method to solve the small problem are often the same method, the function calls itself. In addition, the function to solve the problem must have an obvious ending condition, so that there will be no infinite recursion.
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/2314.