Recursion:
Definition: A function calls itself directly or indirectly
(Do not write dead recursion)
Example:
1. The 1+2+3+4+...+100 and
- Find factorial
- Hanoi
- Walk the Maze
function is called (in C language as an example)
When another function is called during the run of a function, the system needs to complete three things before running the called function:
- Pass all the actual parameters, return the address (that is, the location of the calling function, wait until the call function is complete, return to this position, continue executing the following code), and so on to the function saved
- Allocating storage space for local variables (also including formal parameters) of the modulated function
- Transfer control to the entry of the modulated function
There are three things the system has to do before returning the function from the tuned functions:
- Save the returned result of the tuned function
- releasing the space occupied by the function being tuned (refers to the static space, not including the dynamically allocated space of malloc)
- Transfers control to the calling function according to the return address saved by the tuned function
when more than one function calls each other, according to the principle of "return first after call", the information transfer and control transfer between the above functions must be realized by means of "stack", that is, the system arranges the data space required by the whole program to run in a stack, and allocates a storage area at the top of the stack whenever a function is called. , the stack operation, every time a function exits, the release of its storage area, the stack operation, the current function is always on the top of the stack position.
Conditions that must be met for recursion
- Recursion must have a definite termination condition.
- The size of the data that the function handles must be decremented
- This transformation must be solvable.
Loops and recursion
Recursive
Easy to understand
Slow speed
Large storage space
Cycle
Not to understand
Fast speed
Small storage space
Hanoi Pseudo-Algorithm
if (n > 1)
{
first put The first n-1 plate on a pillar moves from A to B with C
will be The nth plate on column A is moved directly to C.
then the N-1 a plate on the B pillar move to C with a
}
#include <stdio.h>//Hanoi HanoivoidHanoiintNCharACharBCharC) { /*if it's 1 plates, move the plate on the A pillar directly from a to C, then move the n-1 plate on the A pillar with C to B to move the plate on the A pillar directly from a to C and finally the n-1 plate on the B pillar to C. */ if(n = =1) {printf ("move the plate numbered%d directly from the%c pillar to the%c pillar \ n", N, A, C); } Else{Hanoi (n-1, A, C, B); printf ("move the plate numbered%d directly from the%c pillar to the%c pillar \ n", N, A, C); Hanoi (n-1, B, A, C); }}intMainvoid){ CharCH1 ='A'; CharCH2 ='B'; CharCH3 ='C'; intN; printf ("Please enter the number of plates to be moved:"); scanf ("%d", &N); Hanoi (N,'A','B','C'); return 0;}
Recursive applications:
Number and forest
Many algorithms for numbers and graphs are implemented recursively
Many mathematical formulas
Recursion of data structure and algorithm