Data structure and algorithm-visual comprehension of recursion

Source: Internet
Author: User

FIB (n) = 1 (n=1)

FIB (n) = N*fib (n-1) (n>1)

If the condition is not true, continue calling the function and check that the condition is not satisfied then continue calling the function ... Until the function returns a value of 1 o'clock, another layer returns the return value recursively.

We can dissect those complex algorithms with the simplest possible examples of conditions.

Example: 5 * 4 * 3 * 2 * 1 =?

Well, the above test numbers are too big, too complicated, and then choose a simple point for example: 3 * 2 * 1 =?

Some say 2 * 1 =? More simply, we are going to embody the recursive nature, so choose 3 * 2 * 1 =? No more suitable, no more nonsense, get to the point:

The recursive algorithm source code is:

uint32_t fib (uint32_t N)

{

if (1==n)

return 1;

Else

Return (n * FIB (n-1));

}

Test source for (because the statement embedded level is deep, so we choose the smallest example to analyze, please follow the following statement analysis, you will be enlightened! ):

FIB (3)

{

if (1 = = 3)

return 1;

Else

Return (3 * FIB (3-1));

}

The source code expands to:

For the first reading note, see note 1, and look at note 2 According to the following comment hint:

FIB (3)/* NOTE 2: Finally get fib (3) = 6, this is the idea of recursive algorithm, it is like peeling onion skin layer of deep operations to the most tender and delicious layer, and then return the value of a layer back up * *

{

if (1 = = 3)

return 1;

Else

Return (3 * FIB (3-1)); Note 1: here n = = Incoming parameter 3

Expand at this statement:

3 * FIB (2)//2: According to FIB (2) = 2, the FIB (3) = = return (3 * FIB (2)) = = Return (3 * 2) = = return 6; So fib (3) = 6;

{

if (1==2)

return 1;

Else

Return (2 * FIB (2-1)); NOTE 2: So the return value here is: Return (2 * FIB (1)) = = Return (2 * 1) = = return (2); fib (2) = 2

Expand at this statement:

2 * FIB (1)//Note 2:2 * FIB (1) = 2 * 1 = 2, then go up to see return (2 * FIB (2-1)); Note 2

{

if (1==1) return 1; Incoming parameter n is 1, the condition is set! The FIB (1) will return 1, so be aware of the "Note 2" Above 2 * FIB (1)

else return (1 * FIB (1)); Obviously not going to be executed here

}

}

}

Data structure and algorithm-visual comprehension of recursion

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.