C language recursive learning

C language recursion

Recursion is a very important programming technique in C language, which enables programs to solve some problems more concisely and elegantly.

The basic idea of ​​recursion is to decompose a large problem into several small problems until the problem is small enough to be solved directly. In C language, recursive function is the main tool to realize this idea.

A typical recursive function consists of two parts: the base case and the recursive case. The basic situation refers to the situation where the problem is small enough to be solved directly; the recursive situation refers to the problem that has not reached the basic situation, and the problem needs to be continued to be decomposed by calling the function itself.

Here is an example of a simple recursive function that computes factorial:

int factorial(int n)
{
    if (n == 0) {
        return 1; // 基本情况
    } else {
        return n * factorial(n - 1); // 递归情况
    }
}

In this function, when n is 0, the function returns 1 directly, which is the base case. When n is not 0, the function calls itself to calculate the factorial of n-1, and then multiplies n by the calculated result, which is the recursive case.

It should be noted that recursive functions may cause problems such as stack overflow, so you need to be careful when using recursion, especially when dealing with large-scale data.

In embedded systems, recursion, although not as common as it is in general software development, has practical applications. For example, in an operating system kernel, the implementation of some data structures may require the use of recursion. For example, in the Linux kernel, the inode node of the virtual file system is implemented using a recursive structure. In addition, in some embedded system drivers, recursive algorithms may also be used to traverse certain data structures, such as tree structures.

As a simple example, suppose we want to write a function that traverses a binary tree and prints the value of each node. We can use a recursive algorithm to traverse from the root node, first output the value of the current node, and then recursively traverse the left and right subtrees. The code example is as follows:

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};

void inorderTraversal(struct TreeNode* root) {
    if (root == NULL) {
        return;
    }
    inorderTraversal(root->left);
    printf("%d ", root->val);
    inorderTraversal(root->right);
}

In this function, if the current node is empty, the function returns directly; otherwise, the function first traverses the left subtree, then outputs the value of the current node, and finally traverses the right subtree. In this way, the function of inorder traversal of the binary tree can be realized.

It should be noted that in embedded systems, recursive algorithms may occupy a large amount of stack space, so special care is required. When dealing with large-scale data, it is best to avoid using recursive algorithms, or use non-recursive algorithms for optimization.

Here is an example of a recursive function that computes the factorial of a number:

int factorial(int n)
{
    if (n == 0) {
        return 1; // 基本情况
    } else {
        return n * factorial(n - 1); // 递归情况
    }
}

In this function, when n is 0, the function returns 1 directly, which is the base case. When n is not 0, the function calls itself to calculate the factorial of n-1, and then multiplies n by the calculated result, which is the recursive case.

The Fibonacci sequence means that the nth number is obtained by adding the first two numbers, where the first number is 0 and the second number is 1. That is, F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) (n>=2). Here is an example of a function that calculates the Fibonacci sequence recursively:

int fibonacci(int n)
{
    if (n == 0) {
        return 0; // 基本情况
    } else if (n == 1) {
        return 1; // 基本情况
    } else {
        return fibonacci(n - 1) + fibonacci(n - 2); // 递归情况
    }
}

In this function, when n is 0 or 1, the function directly returns the corresponding value, which is the basic case. When n is not 0 or 1, the function calls itself to calculate the Fibonacci sequence values ​​of n-1 and n-2, and then adds the two values, which is the recursive case.

It should be noted that recursive functions may cause problems such as stack overflow, so you need to be careful when using recursion, especially when dealing with large-scale data. In embedded systems, recursion, although not as common as it is in general software development, has practical applications. For example, in an operating system kernel, the implementation of some data structures may require the use of recursion. In addition, in some embedded system drivers, recursive algorithms may also be used to traverse certain data structures, such as tree structures.

Guess you like

Origin blog.csdn.net/weixin_51624736/article/details/129592477