Recursion of PHP Data structure basics

Source: Internet
Author: User
Tags gcd
This article mainly introduces the PHP data structure based on recursion, has a certain reference value, now share to everyone, have the need for friends can refer to

What is recursion?

As mentioned before, recursion is a solution that breaks down big problems into small problems. In general, recursion is called the function itself. That might sound strange, in fact, in recursion, the function really has to call itself.

A chestnut

In mathematics, for example, we all know the concept of "factorial". For example, the factorial of 5 is 5*4*3*2*1 .

    • 5! = 5 * 4!

    • 4! = 4 * 3!

    • 3! = 3 * 2!

    • 2! = 2 * 1!

    • 1! = 1 * 0!

    • 0! = 1

We can sum up the law of finding the factorial of N, that is n! = n * (n-1)!

This is the embodiment of recursion. As you can see, we turn the factorial of the 5 into another small problem in one step.

Characteristics of recursive algorithms

    • Each recursive call must be based on a small sub-problem. For example, the factorial of 5 is the factorial of 5 by 4.

    • Recursion must have a base case. For example, the base case of factorial is 0, and when the condition is 0, the recursion is stopped.

    • Avoid cyclic calls in recursion, or the last computer will show stack overflow errors.

function factorial (int $n): int{    if ($n = 0) {        return 1;    }        return $n * Factorial ($n-1);}

Looking at the code above, we can see the solution to the factorial problem we have a basic condition is that when n is 0, we return 1. If this condition is not met, we return the n multiplication factorial(n) , which conforms to the first and third of the recursive characteristics. We avoided looping calls because we decomposed each recursive call into a small sub-problem of a big problem. The above algorithm thought can be expressed as:

Recursive vs Iteration

The recursive code above can also be implemented using an iterative approach

function factorial (int $n): int{    $result = 1;        for ($i = $n; $i > 0; $i-) {        $result *= $n;    }        return $result;}

If an issue can be easily solved using iterations, why should we use recursion?

Recursion is used to deal with more complex problems, and not all problems can be solved simply by using iterations. Recursively uses function calls to manage the call stack, so more and more time and memory are used than iterative recursion. Also, in an iteration, we have a result for each step, but in recursion we have to wait until the base case execution finishes to have any results. Looking at the example above, we found that in the recursive algorithm we do not have any variables or declarations to save the results, and in the iterative algorithm, we use $result to save the returned results every time.

Fibonacci sequence

In mathematics, the Fibonacci sequence is a special integer sequence, and each number in the sequence is generated by a sum of two additional numbers. The rules are as follows:

function Fibonacci ($n) {    if ($n = = 0) {        return 0;    }        if ($n = = 1) {        return 1;    }        Return Fibonacci ($n-1) + Fibonacci ($-2);}

Maximum male factor

Another common problem with recursive algorithms is to find the maximum common factor of two numbers.

function gcd (int $a, int $b) {    if ($b = = 0) {        return $a;    }        Return gcd ($b, $a% $b);}

Recursive type

    • Linear recursion

In each recursive call, the function calls itself only once, which is called linear recursion.

    • Two-point recursion

In binary recursion, each recursive call function calls itself two times. The algorithm for solving the Fibonacci sequence is binary recursion, in addition to the binary search, divide and conquer algorithm, merge sort, etc. also used two points recursion.

    • Tail recursion

A recursive return is called a tail recursion when there is no waiting operation. In the Fibonacci algorithm, the return value needs to be multiplied by the previous recursive return value, so he is not the tail recursion, and the algorithm that solves the maximum common divisor is the tail recursion. Tail recursion is a form of linear recursion.

    • Reciprocal recursion

For example, in each recursive call there is a () call B (), B () call a (), such recursion is called reciprocal recursion.

    • Nested recursion

When a recursive function calls itself recursively as a parameter, it is called nested recursion. A common chestnut is the Ackermann function, which looks at the following expression.

Looking at the last line, you can see that the second parameter is the recursive function itself.

Next section

The next article uses recursion to solve some of the problems that are encountered in real-world development, such as building n-level classifications, building nested comments, traversing directory files, and so on.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.