Recursion, function calling mechanism and tower of arguments

Source: Internet
Author: User

Recursion

Recursion a function calls its own method directly or indirectly. It usually converts a large and complex problem into a small problem similar to the original problem to solve it. Recursion must meet three conditions: first, recursion must have a clear Stop condition, otherwise it is likely to fall into dead recursion; second, the data (or problems) processed by recursive functions) the scale must be decreasing. The solution of n problems depends on the solution of n-1 problems. The third is that this transformation must be feasible.

Function call Mechanism

When calling another function during the operation of a function, the system needs to complete three tasks before running the called function:
A) pass all the actual parameters, return addresses (the address of the next statement), and other information to the called function for storage;
B). Allocate storage space for local variables (including parameters) of the called function;
C) Transfer the control permission to the entry of the called function;
Before the called function returns the main function, the system must complete three tasks:
A>. Save the returned results of the called function;
B>. Release the storage space occupied by the called function;
C>. Transfer the control permission to the call function based on the return address saved by the called function.
When multiple functions are called to each other, according to the principle of "return first after call", information transfer and permission transfer control between the above functions must be implemented through

Stack, that is, the system arranges the data space required for the entire program to run in one stack. Every time a function is called

Top allocates a storage area for the stack operation; each time a function exits, it releases its storage area, performs the stack operation, and is currently running

The function is always at the top of the stack!
In computer opinion, function A calls function A and function A calls function B in essence. Each time a function is called, the pressure stack operation is performed.

When a function exits, its storage zone will be released and the current running function will always be at the top of the stack.

Comparison of loops and Recursion

Recursive advantages: easy to understand (The problem scale can be converted to a problem with smaller scope)

Recursion disadvantages: Slow speed (determined by the function call mechanism, You have to tune yourself for each recursion), large storage space (a large waste of space)

Advantages of circulation: relatively fast, small storage space (small waste of space)

Loop disadvantages: Hard to understand (many algorithms need to be pushed one by one)

All loops can be implemented recursively, but not all recursion can be implemented cyclically.

Tower of Hanoi Problems

Question Description: how to move n plates on column A (the first column) to column C (the third column) with column B (the second column ?, Only one plate can be moved at a time. During the movement, the Small and Medium plates must always be placed on the large plate (the bottom plate number is the largest ).

Pseudo algorithm: 1. When there is only one plate on column A, you can directly move the plate on column A to column C.

2. When the number of plates on the column A is n (n> 1), we can solve the problem in three steps by using recursive thinking:

1) Move n-1 plates on column A to Column B with column C;

2) directly move the n plate on column A to column C;

3) Move n-1 plates on column B to column C with column;

Recursive Algorithm Implementation (requires at least one plate on the column ):

 

# Include <stdio. h> void Hanoitower (int n, char A, char B, char C) {if (1 = n) {printf ("Move the plate numbered % d from the % c column to the % c column \ n", n, A, C);} else {Hanoitower (n-1,, c, B); printf ("Move the plate numbered % d from the % c column to the % c column \ n", n-1, A, C); Hanoitower (n-1, B, a, C) ;}} int main () {int n; printf ("Enter the number of plates:"); scanf ("% d", & n ); hanoitower (n, 'A', 'B', 'C'); return 0 ;}# include <stdio. h> void Hanoitower (int n, char A, char B, char C) {if (1 = n) {printf ("Move the plate numbered % d from the % c column to the % c column \ n", n, A, C);} else {Hanoitower (n-1,, c, B); printf ("Move the plate numbered % d from the % c column to the % c column \ n", n-1, A, C); Hanoitower (n-1, B, a, C) ;}} int main () {int n; printf ("Enter the number of plates:"); scanf ("% d", & n ); hanoitower (n, 'A', 'B', 'C'); return 0 ;}

It can be calculated from the number of plate transfers: the complexity of the tower is the Npower of 2-1.

Conclusion

Recursion is used in many places. For example, trees and graphs are defined in recursion mode. Recursion provides us with a way to solve complex problems (but this is basically a mathematical problem, so it is sometimes difficult to understand its logic ). Learning linear structures (arrays, linked lists and their application stacks, queues, etc.), recursion, and their ideas are mainly used to convert non-linear problems into linear problems in the future, then, the nonlinear structure problem is solved through linear structure methods and ideas.

 

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.