我在学校里很难理解递归。每当教授谈到它的时候,我似乎都懂了,但当我自己尝试的时候,它完全震撼了我的大脑。

我整晚都在尝试着解决《Towers of Hanoi》这款游戏,结果完全出乎我的意料。我的课本只有大约30页关于递归的内容,所以它不是很有用。有人知道哪些书籍或资源可以帮助澄清这个话题吗?


当前回答

这与其说是一个问题,不如说是一个抱怨。关于递归你有更具体的问题吗?就像乘法一样,人们不会写很多关于它的东西。

说到乘法,想想这个。

问题:

* b是什么?

答:

如果b = 1,就是a。 否则就是a+a*(b-1)

* (b - 1)是什么?请参考上面的问题来解决这个问题。

其他回答

递归函数就像弹簧,每次调用都要压缩一点。在每一步中,您将一些信息(当前上下文)放在堆栈上。当到达最后一步时,释放弹簧,立即收集所有值(上下文)!

不确定这个比喻是否有效…: -)

无论如何,除了经典的例子(阶乘是最糟糕的例子,因为它效率低,很容易被平化,Fibonacci, Hanoi…),这些都有点人为(我很少,如果有的话,在实际编程案例中使用它们),看看它真正被使用的地方是有趣的。

A very common case is to walk a tree (or a graph, but trees are more common, in general). For example, a folder hierarchy: to list the files, you iterate on them. If you find a sub-directory, the function listing the files call itself with the new folder as argument. When coming back from listing this new folder (and its sub-folders!), it resumes its context, to the next file (or folder). Another concrete case is when drawing a hierarchy of GUI components: it is common to have containers, like panes, to hold components which can be panes too, or compound components, etc. The painting routine calls recursively the paint function of each component, which calls the paint function of all the components it holds, etc.

不确定我是否很清楚,但我喜欢展示现实世界中教材的使用,因为这是我过去偶然发现的东西。

当使用递归解时,我总是尝试:

首先建立基本情况,即。 当n = 1的阶乘 试着想出一个一般的规则 对于其他情况

还有不同类型的递归解,分而治之的方法对分形和其他方法很有用。

如果你能先解决一些简单的问题,只是为了掌握窍门,这也会有帮助。一些例子是求解阶乘和生成第n个斐波那契数。

作为参考,我强烈推荐Robert Sedgewick的算法。

希望这能有所帮助。祝你好运。

你如何倒空一个装有五朵花的花瓶?

答:如果花瓶不是空的,你就拿出一朵花 然后你倒空了一个装有四朵花的花瓶。

你如何倒空一个装有四朵花的花瓶?

答:如果花瓶不是空的,你就拿出一朵花 然后你倒空了一个装有三朵花的花瓶。

你如何倒空一个装有三朵花的花瓶?

答:如果花瓶不是空的,你就拿出一朵花 然后你把装着两朵花的花瓶倒空。

你如何倒空一个装有两朵花的花瓶?

答:如果花瓶不是空的,你就拿出一朵花 然后你把只装了一朵花的花瓶倒空。

你如何清空只装了一朵花的花瓶?

答:如果花瓶不是空的,你就拿出一朵花 然后你倒空了一个没有花的花瓶。

你怎么倒空一个没有花的花瓶?

答:如果花瓶不是空的,你就拿出一朵花 但是花瓶是空的,所以你完成了。

这是重复的。让我们概括一下:

你如何倒空一个装有N朵花的花瓶?

答:如果花瓶不是空的,你就拿出一朵花 然后你倒空一个装有N-1朵花的花瓶。

嗯,我们能看看代码吗?

void emptyVase( int flowersInVase ) {
  if( flowersInVase > 0 ) {
   // take one flower and
    emptyVase( flowersInVase - 1 ) ;

  } else {
   // the vase is empty, nothing to do
  }
}

我们不能在for循环中做这个吗?

为什么,是的,递归可以用迭代代替,但通常递归更优雅。

让我们来谈谈树。在计算机科学中,树是由节点组成的结构,其中每个节点都有一定数量的子节点,这些子节点也是节点或null。二叉树是由恰好有两个子结点组成的树,通常称为“左”和“右”;同样,子节点可以是节点,也可以为空。根节点不是任何其他节点的子节点。

假设一个节点,除了它的子节点外,还有一个值,一个数字,然后假设我们想要在某个树中对所有的值求和。

为了对任意一个节点的值求和,我们将节点本身的值与它左子节点的值(如果有的话)和右子节点的值(如果有的话)相加。现在回想一下,如果子节点不为空,它们也是节点。

因此,要对左子节点求和,我们需要将子节点自身的值与左子节点的值(如果有的话)以及右子节点的值(如果有的话)相加。

因此,要对左子节点的左子节点的值求和,我们需要将子节点自身的值与它的左子节点的值(如果有的话)以及它的右子节点的值(如果有的话)相加。

也许您已经预料到我要做什么,并希望看到一些代码?好:

struct node {
  node* left;
  node* right;
  int value;
} ;

int sumNode( node* root ) {
  // if there is no tree, its sum is zero
  if( root == null ) {
    return 0 ;

  } else { // there is a tree
    return root->value + sumNode( root->left ) + sumNode( root->right ) ;
  }
}

请注意,我们没有显式地测试子节点以确定它们是空节点还是节点,而是让递归函数对于空节点返回零。

假设我们有一个这样的树(数字是值,斜杠指向子结点,@表示指针指向null):

     5
    / \
   4   3
  /\   /\
 2  1 @  @
/\  /\
@@  @@

如果我们在根节点(值为5的节点)上调用sumNode,我们将返回:

return root->value + sumNode( root->left ) + sumNode( root->right ) ;
return 5 + sumNode( node-with-value-4 ) + sumNode( node-with-value-3 ) ;

我们把它展开。在任何我们看到sumNode的地方,我们都将它替换为return语句的展开:

sumNode( node-with-value-5);
return root->value + sumNode( root->left ) + sumNode( root->right ) ;
return 5 + sumNode( node-with-value-4 ) + sumNode( node-with-value-3 ) ;

return 5 + 4 + sumNode( node-with-value-2 ) + sumNode( node-with-value-1 ) 
 + sumNode( node-with-value-3 ) ;  

return 5 + 4 
 + 2 + sumNode(null ) + sumNode( null )
 + sumNode( node-with-value-1 ) 
 + sumNode( node-with-value-3 ) ;  

return 5 + 4 
 + 2 + 0 + 0
 + sumNode( node-with-value-1 ) 
 + sumNode( node-with-value-3 ) ; 

return 5 + 4 
 + 2 + 0 + 0
 + 1 + sumNode(null ) + sumNode( null )
 + sumNode( node-with-value-3 ) ; 

return 5 + 4 
 + 2 + 0 + 0
 + 1 + 0 + 0
 + sumNode( node-with-value-3 ) ; 

return 5 + 4 
 + 2 + 0 + 0
 + 1 + 0 + 0
 + 3 + sumNode(null ) + sumNode( null ) ; 

return 5 + 4 
 + 2 + 0 + 0
 + 1 + 0 + 0
 + 3 + 0 + 0 ;

return 5 + 4 
 + 2 + 0 + 0
 + 1 + 0 + 0
 + 3 ;

return 5 + 4 
 + 2 + 0 + 0
 + 1 
 + 3  ;

return 5 + 4 
 + 2 
 + 1 
 + 3  ;

return 5 + 4 
 + 3
 + 3  ;

return 5 + 7
 + 3  ;

return 5 + 10 ;

return 15 ;

现在看看我们是如何通过将其视为复合模板的重复应用来征服任意深度和“分支性”的结构的。每次通过我们的sumNode函数,我们只处理一个节点,使用一个单一的if/then分支,和两个简单的返回语句,几乎自己写,直接从我们的规范?

How to sum a node:
 If a node is null 
   its sum is zero
 otherwise 
   its sum is its value 
   plus the sum of its left child node
   plus the sum of its right child node

这就是递归的力量。


上面的花瓶例子是尾递归的一个例子。尾递归的意思是,在递归函数中,如果我们递归(也就是说,如果我们再次调用这个函数),这是我们做的最后一件事。

树的例子不是尾递归,因为尽管我们最后做的是递归右子结点,但在这之前我们递归了左子结点。

事实上,我们调用子节点的顺序,以及添加当前节点值的顺序根本不重要,因为加法是可交换的。

现在让我们来看一个顺序很重要的操作。我们将使用节点的二叉树,但这次保存的值将是一个字符,而不是一个数字。

我们的树将有一个特殊的属性,即对于任何节点,其字符都在其左子字符之后(按字母顺序),在其右子字符之前(按字母顺序)。

我们要做的是按字母顺序打印树。考虑到树的特殊性质,这很容易做到。我们只打印左子节点,然后是节点的字符,然后是右子节点。

我们不想随意地打印,所以我们会给函数传递一些东西来打印。这将是一个带有print(char)函数的对象;我们不需要担心它是如何工作的,只需要知道当print被调用时,它会在某个地方打印一些东西。

让我们看看代码:

struct node {
  node* left;
  node* right;
  char value;
} ;

// don't worry about this code
class Printer {
  private ostream& out;
  Printer( ostream& o ) :out(o) {}
  void print( char c ) { out << c; }
}

// worry about this code
int printNode( node* root, Printer& printer ) {
  // if there is no tree, do nothing
  if( root == null ) {
    return ;

  } else { // there is a tree
    printNode( root->left, printer );
    printer.print( value );
    printNode( root->right, printer );
}

Printer printer( std::cout ) ;
node* root = makeTree() ; // this function returns a tree, somehow
printNode( root, printer );

除了运算的顺序现在很重要之外,这个例子还说明了我们可以将东西传递到递归函数中。我们要做的唯一一件事就是确保在每次递归调用时,我们继续传递它。我们将一个节点指针和一个打印机传递给函数,在每次递归调用时,我们将它们“向下”传递。

如果我们的树是这样的

         k
        / \
       h   n
      /\   /\
     a  j @  @
    /\ /\
    @@ i@
       /\
       @@

我们要打印什么?

From k, we go left to
  h, where we go left to
    a, where we go left to 
      null, where we do nothing and so
    we return to a, where we print 'a' and then go right to
      null, where we do nothing and so
    we return to a and are done, so
  we return to h, where we print 'h' and then go right to
    j, where we go left to
      i, where we go left to 
        null, where we do nothing and so
      we return to i, where we print 'i' and then go right to
        null, where we do nothing and so
      we return to i and are done, so
    we return to j, where we print 'j' and then go right to
      null, where we do nothing and so
    we return to j and are done, so
  we return to h and are done, so
we return to k, where we print 'k' and then go right to
  n where we go left to 
    null, where we do nothing and so
  we return to n, where we print 'n' and then go right to
    null, where we do nothing and so
  we return to n and are done, so 
we return to k and are done, so we return to the caller

如果我们只看打印出来的行

    we return to a, where we print 'a' and then go right to
  we return to h, where we print 'h' and then go right to
      we return to i, where we print 'i' and then go right to
    we return to j, where we print 'j' and then go right to
we return to k, where we print 'k' and then go right to
  we return to n, where we print 'n' and then go right to

我们看到我们打印了“ahijkn”,它确实是按字母顺序排列的。

我们只需知道如何按字母顺序打印单个节点,就可以按字母顺序打印整个树。这只是(因为我们的树具有将值按字母顺序排在后面的值的左边的特殊属性)知道在打印节点的值之前打印左子节点,在打印节点的值之后打印右子节点。

这就是递归的力量:能够通过只知道如何做整体的一部分来完成整个事情(并且知道何时停止递归)。

回想一下,在大多数语言中,操作符|| ("or")在第一个操作数为真时短路,一般递归函数为:

void recurse() { doWeStop() || recurse(); } 

Luc M评论道:

SO应该为这种回答创建一个徽章。恭喜你!

谢谢,吕克·!但实际上,因为我编辑了不止四次这个答案(为了添加最后一个例子,但主要是为了纠正错字和润色——在一个小小的上网本键盘上打字很困难),我无法从这个答案中获得更多的分数。这在一定程度上阻碍了我在未来的答案上花那么多精力。

请看我的评论:https://stackoverflow.com/questions/128434/what-are-community-wiki-posts-in-stackoverflow/718699#718699

递归

方法A调用方法A调用方法A,最终这些方法A中的一个不会调用并退出,但这是递归,因为有东西调用了它自己。

递归的例子,我想打印出硬盘驱动器上的每个文件夹名称:(在c#中)

public void PrintFolderNames(DirectoryInfo directory)
{
    Console.WriteLine(directory.Name);

    DirectoryInfo[] children = directory.GetDirectories();

    foreach(var child in children)
    {
        PrintFolderNames(child); // See we call ourself here...
    }
}

http://javabat.com是一个有趣而令人兴奋的练习递归的地方。他们的例子开始时相当简单,然后逐步扩展(如果你想这么做的话)。注意:他们的方法是在实践中学习。这是我写的一个递归函数,用来替换for循环。

for循环:

public printBar(length)
{
  String holder = "";
  for (int index = 0; i < length; i++)
  {
    holder += "*"
  }
  return holder;
}

这是做同样事情的递归。(请注意,我们重载了第一个方法,以确保它像上面那样使用)。我们还有另一种方法来维护索引(类似于上面的for语句)。递归函数必须维护自己的索引。

public String printBar(int Length) // Method, to call the recursive function
{
  printBar(length, 0);
}

public String printBar(int length, int index) //Overloaded recursive method
{
  // To get a better idea of how this works without a for loop
  // you can also replace this if/else with the for loop and
  // operationally, it should do the same thing.
  if (index >= length)
    return "";
  else
    return "*" + printBar(length, index + 1); // Make recursive call
}

简而言之,递归是一种编写更少代码的好方法。在后面的printBar中,请注意我们有一个if语句。如果我们的条件已经达到,我们将退出递归并返回到前一个方法,该方法返回到前一个方法,等等。如果我发送一个printBar(8),我得到********。我希望通过一个简单函数的例子,它做的事情与for循环相同,这可能会有所帮助。不过,您可以在Java Bat中进行更多的练习。