About Recursion

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.  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值