Data structure --- recursion

Recursion

The importance of recursion for programming

  • Recursion is a qualitative leap for your coding ability. If you want to become a very powerful programmer, data structure must be mastered . Computers are especially suitable for solving problems with recursive thinking, but we humans use recursive thinking to consider The problem will be very troublesome, and this is what many people who have studied recursion have never figured out! The idea of ​​recursion is one of the basic ideas of software thinking . In tree and graph theory , almost all are realized by recursion. The simplest problem, such as finding the factorial, which has no clear number of executions, is solved by recursion.

Definition of recursion

  • A function calls itself directly or indirectly (a function calling another function is exactly the same as when it calls itself, they are all three steps, but it is a bit weird in the eyes of people.)

Detailed explanation of function calls

When calling another function during the running of a function, the system needs to complete three things before running the called function:

  1. Pass all the actual parameters, return address and other information to the called function to save;
  2. Allocate storage space for local variables (including formal parameters) of the called function;
  3. Transfer control to the entry of the called function.

Before returning from the called function to the calling function , the system must complete three things:

  1. Save the return result of the called function;
  2. Release the storage space occupied by the called function;
  3. Transfer control to the calling function according to the return address saved by the called function.

When there are multiple functions calling each other, according to the principle of " " after the call returns "", the information transfer and control transfer between the above functions must be realized with the ""stack" ", that is, the system needs to run the entire program The data space is arranged in a stack. Whenever a function is called, a storage area is allocated on the top of the stack and the stack operation is performed . Whenever a function exits, its storage area is released and the stack operation is performed . The current operation The function is always at the top of the stack.

From the computer's point of view, there is no difference between A function calling A function and A function calling B function, but it is weird to understand in our daily way of thinking!

Three conditions satisfied recursively

  1. Recursion must have a clear termination condition
  2. The scale of data processed by this function must be decreasing
  3. This transformation must be solvable

Comparison of loop and recursion

Recursion:

  • Easy to understand
  • Slow
  • Large storage space

cycle:

  • Not easy to understand
  • high speed
  • Little storage space

Theoretically, what can be solved by a cycle can definitely be transformed into recursion, but this process is a complex mathematical transformation process. Recursion can be solved not necessarily into a cycle. Whether you have the ability to use it depends on the individual.

Solve the Tower of Hanoi Problem with Recursion

Insert picture description here
Pseudo algorithm:

if(n > 1)
{
    
    
	//先把A柱子上的前n-1个盘子从A借助C移动B
	//将A柱子上的第n个盘子直接移到C
	//再讲B柱子上的n-1个盘子借助A移到C
}

Implementation:

#include <stdio.h>
#include <stdlib.h>

void hannuota(int n,char A,char B,char C)
{
    
    
/*如果是一个盘子,直接从A柱子上的盘子从A移到C
否则,将A柱子上的n-1个盘子借助C移到B.直接将A柱子上的盘子从A移到C,
最后将B柱子上的n-1个盘子借助A移到C*/

    if(1 == n)
        printf("将编号为%d的盘子直接从%c柱子移到%c柱子\n",n,A,C);
    else
    {
    
    
        hannuota(n-1,A,C,B);
        printf("将编号为%d的盘子直接从%c柱子移到%c柱子\n",n,A,C);
        hannuota(n-1,B,A,C);
    }
}

int main()
{
    
    
    char ch1 = 'A';
    char ch2 = 'B';
    char ch3 = 'C';
    int n;

    printf("请输入盘子的个数:");
    scanf("%d",&n);

    hannuota(n,'A','B','C');

    return 0;
}

Guess you like

Origin blog.csdn.net/qq_41782149/article/details/92843197