本想先找本算法和数据结构的书参考一下,但转念一想:不如考考自己的总结和逻辑表达能力。

loop、iterate、traversal和recursion这几个词是计算机技术书中经常会出现的几个词汇。众所周知,这几个词分别翻译为:循环、迭代、遍历和递归。乍一看,这几个词好像都与重复(repeat)有关,但有的又好像不完全是重复的意思。那么这几个词到底各是什么含义,有什么区别和联系呢?下面就试着解释一下。

  • 循环(loop),指的是在满足条件的情况下,重复执行同一段代码。比如,while语句。
  • 迭代(iterate),指的是按照某种顺序逐个访问列表中的每一项。比如,for语句。
  • 遍历(traversal),指的是按照一定的规则访问树形结构中的每个节点,而且每个节点都只访问一次。
  • 递归(recursion),指的是一个函数不断调用自身的行为。比如,以编程方式输出著名的斐波纳契数列。

有了以上定义,这几个概念之间的区别其实就比较清楚了。至于它们之间的联系,严格来讲,它们似乎都属于算法的范畴。换句话说,它们只不过是解决问题的不同手段和方式,而本质上则都是计算机编程中达成特定目标的途径。



朋友们的留言

  1. George Wing | 08月 10th, 2010 at 11:00

    学习了!赞!

    Reply to this comment
  2. mort | 12月 5th, 2010 at 10:28

    对迭代的解释好像不太准确?

    Reply to this comment
  3. mort | 12月 13th, 2010 at 23:05

    指教不敢当。只是感觉好像有些不妥。
    比如计算 1…n的 和。

    迭代方式:
    int sum_iterative(int n){
    int result=0;
    for(int i=1;i<=n;++i){
    result+=i;
    }
    retrun n;
    }

    递归方式:
    int sum_recursive(int n){
    return n==1?n:n+sum_recursive(n-1);
    }

    这里面“迭代”不符合你的定义中说的对列表的逐一访问。
    我觉得迭代更多的时候,是相对递归来说的。
    迭代是反复重复一个过程,直到得出解。
    而递归则是把问题转化成一个更小的问题来求解。

    不知道这样理解是否准确?

    Reply to this comment
  4. mort | 12月 13th, 2010 at 23:07

    指教不敢当。只是说说我的看法。

    举个例子
    比如计算 1…n的 和。

    迭代方式:
    int sum_iterative(int n){
    int result=0;
    for(int i=1;i<=n;++i){
    result+=i;
    }
    retrun result;
    }

    递归方式:
    int sum_recursive(int n){
    return n==1?n:n+sum_recursive(n-1);
    }

    这里面“迭代”不符合你的定义中说的对列表的逐一访问。
    我觉得迭代更多的时候,是相对递归来说的。
    迭代是反复重复一个过程,直到得出解。
    而递归则是把问题转化成一个更小的问题来求解。

    不知道这样理解是否准确?

    Reply to this comment
  5. rock crusher | 01月 7th, 2011 at 09:38

    很多不明白,呵呵

    Reply to this comment
  6. 循环、迭代、遍历和递归 | w3er | 05月 15th, 2011 at 12:45

    [...] http://www.cn-cuckoo.com/2010/08/09/loop-iterate-traversal-and-recursion-1846.html 此条目发表在 未分类 分类目录,贴了 原创, 翻译 [...]

    Reply to this comment
  7. 循环、迭代、遍历和递归 | w3er | 05月 16th, 2011 at 00:21

    [...] http://www.cn-cuckoo.com/2010/08/09/loop-iterate-traversal-and-recursion-1846.html 此条目发表在 转载 分类目录,贴了 原创, 翻译 [...]

    Reply to this comment
  8. usavps | 07月 23rd, 2011 at 12:13

    找了这么多资料,这是解释的最清楚的。

    Reply to this comment
  9. vacation packages | 09月 16th, 2011 at 00:37

    Woah! I’m really digging the template/theme of this blog. It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between usability and appearance. I must say you have done a fantastic job with this. Additionally, the blog loads extremely quick for me on Internet explorer. Superb Blog!

    Reply to this comment
  10. Ollie Imeson | 09月 16th, 2011 at 01:47

    Fantastic weblog. I was checking constantly this site and I am impressed! Very useful information specially the last part I care for such information much.

    Reply to this comment
  11. UGG Sheepskin Boots Australia | 09月 19th, 2011 at 09:18

    very nice post, i certainly love this website, keep on it.

    Reply to this comment
  12. ugg ブーツ | 10月 31st, 2011 at 14:12

    Thanks a lot for sharing this with all of us you really realize what you are speaking approximately! Bookmarked. Thank you!

    Reply to this comment
  13. Office 2010 | 11月 9th, 2011 at 17:24

    You may be more happy than pinces,if you will be more virtuous.

    高房价的房管局

    Reply to this comment
  14. Cheap Jordans | 11月 11th, 2011 at 15:51

    The art work of providing provides would be to give one thing which other people cannot purchase for themselves. (Alan Alexander Milne, British humorist)

    Reply to this comment

我来说两句儿

可以在留言中使用以下标签 :<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree