### 1、 The definition of recursion

If the application of definition itself appears directly (or indirectly) within a function, procedure or data structure definition, they are said to be recursive or recursive.*Recursive function refers to a function that calls itself directly or indirectly through a series of call statements.***The computer uses a stack to record the functions in each call. This stack is called the call stack**：

1. When calling a function, the system will construct an activity record composed of parameter list and return address for the caller, and push it to the top of the runtime stack provided by the system, and then transfer the control of the program to the called function. If the called function has local variables, the corresponding space should be allocated at the top of the stack at runtime. Therefore, the activity record and these local variables form an activity structure that can be used by the called function.

2. When the called function is executed, the system will remove the activity structure at the top of the stack at runtime, and transfer the control of the program to the caller according to the return address saved in the activity structure of the runtime stack.

### 2、 Design steps of recursive algorithm

1. The large-scale original problem is decomposed into one or more smaller subproblems with similar characteristics as the original problem;

2. Determine one or more minimum subproblems that can be solved directly without decomposition (called termination condition or benchmark case of recursion). For example, the factorial of a nonnegative integer n can be recursively defined as:

### 3、 Advantages of recursive algorithm

1. Recursion is a powerful mathematical tool, which can make the description and solution of problems simple and clear.

2. Recursive algorithms are often easier to design than non recursive algorithms, especially when the problem itself or the data structure involved is a recursive definition.

3. Some problems are solved by recursive algorithm, which is ingenious and efficient.

### 4、 Disadvantages of recursive algorithm

In most cases, it is not more elegant and efficient than ordinary loops;

### 5、 Application scenarios of recursive algorithm

1. Recursion works well for those** Unable to predict the depth of calculation**, such as traversing the file system. Suppose you’re going to write a script that does something about all the files in a directory (including files in subdirectories).

2. Fast scheduling algorithm

For details, please refer to the quick sorting algorithm section.

### 6、 Time complexity calculation of recursive algorithm

1、**Substitution method**

(1) The form of guess solution

(2) Find out the constant which makes the solution effective by mathematical induction

For example, let’s solve the recursive formula t (n) = 2T (n / 2) + n,

1-we guess that the solution is O (nlgn), and we need to find a constant C such that t (n) < = cnlgn

2-proof: t (n) < = 2C (n / 2) LG (n / 2) + n < = cnlgn-cnlg2 + n = cnlgn CN + n

As long as C > = 1, t (n) < = cnlgn, our conjecture is correct.

2、**Recursive tree method**

Using recursive tree method to calculate the complexity of the algorithm, in fact, provides a good guess, simple and intuitive. In the recursive tree, each node represents the cost of a single problem, and the subproblem corresponds to a recursive function call. We sum the costs in each layer of the tree to get the cost of each layer, and then sum the costs of all layers to get the total cost of recursive calls at all levels.

According to the above formula, we establish the recursive t (n) = 3T (n / 4) + CN ^ 2, and establish the following recursive tree model

In the recursive tree, each node represents a sub cost. The cost of each layer is the sum of all the sub costs of the layer. The cost of the total problem is the sum of the costs of all layers. Therefore, we use recursive tree to solve the cost, as long as we know the cost of each layer and the number of layers.

As an example, the number of recursions used when recursively calling leaf t (1) is the depth of the whole recursive tree. We can get from the graph that the cost of the nodes in the I level is n / (4 ^ I). When n / (4 ^ I) = 1, i.e. I = log4 (n), the recursion reaches the leaf, so the depth of the whole recursive tree is log4 (n). The total cost is the sum of the costs of all layers, and the result is O (n ^ 2). The calculation process is shown in the introduction to the algorithm.**3. Main method**

**Bibliography**：

1. Data structure and algorithm: C + + Language Edition Author: Xiao Nanfeng, Zhao Jie, etc

2. Data structure and algorithm diagram Author: Jay winglow

3. Introduction to Algorithms