现在的位置: 首页 > 综合 > 正文

About Recursion

2013年08月02日 ⁄ 综合 ⁄ 共 1365字 ⁄ 字号 评论关闭

Reference:  http://www.pureweber.com/article/recursive-power-1/

Recursion is not a simple call itself or interactively control.  The core of recursion is conquer-and- divide. This technique can solve most complex problems, but not all. The problem must satisfy two criteria:

  1) it can be split into multiple small same problems. 

2) it can terminate through a easy exit.

Notice that the termination must be written at the beginning of the function. 

The way of showing results: 1) print alone the procedure 2) print when base case happens: print the base and back tract print all results, or print whole bunch of results if the results are store in a consist data structure one by one.

The deficiency of recursion:

when the steps of calling itself is more than  the real processing, recursion is not a good choice. 

The differences between loop, iterate, recursion and  traversal:

reference: wiki and http://www.cn-cuckoo.com/2010/08/09/loop-iterate-traversal-and-recursion-1846.html

1) Loop: repeat processing same code block, ex: while

2) Iterate:  repetition of a process within a computer program.

3) Recursion: repetition of calling itself

4) Traversal: Travel alone the branches within a tree. 



Tips:

If the program does not have alternative exits, you may use return for just repetition.

If the program has, you may use "void" and other implements like "cout". Or using the function argument to store the state (for recalling the variables' value at the prior stage).


Remember recursion is a kind of implement of the stack,  thus you should use "function argument" to save the variable state.  

 

抱歉!评论已关闭.