Recursion of data structure and algorithm

Source: Internet
Author: User

Recursion:

Definition: A function calls itself directly or indirectly

(Do not write dead recursion)

Example:

1. The 1+2+3+4+...+100 and

    1. Find factorial
    2. Hanoi
    3. 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:

    1. 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
    2. Allocating storage space for local variables (also including formal parameters) of the modulated function
    3. 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:

    1. Save the returned result of the tuned function
    2. releasing the space occupied by the function being tuned (refers to the static space, not including the dynamically allocated space of malloc)
    3. 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

    1. Recursion must have a definite termination condition.
    2. The size of the data that the function handles must be decremented
    3. 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

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.