栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

头尾递归的区别

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

头尾递归的区别

在中

head recursion
,递归调用在发生时先于函数中的其他处理(考虑它发生在函数的顶部或头部)。

在中

tail recursion
,情况恰恰相反—处理发生在递归调用之前。在这两种递归样式之间进行选择似乎是任意的,但是选择可以使一切有所不同。

具有在路径的开头具有单个递归调用的路径的函数使用所谓的头递归。先前展览的阶乘功能使用头部递归。一旦确定需要递归,它所做的第一件事就是使用递减的参数进行调用。在路径末尾具有单个递归调用的函数正在使用尾递归。
请参阅这篇文章

递归示例:

public void tail(int n)         |     public void head(int n){         |     {    if(n == 1)       |         if(n == 0)        return;      |  return;    else  |         else        System.out.println(n);  |  head(n-1);          |    tail(n-1);       |         System.out.println(n);}         |     }

如果递归调用发生在方法的末尾,则称为

tail recursion
。尾递归为
similar to a loop
。的
method executesall the statements before jumping into the next recursive call

如果递归调用发生在

beginning of a method, it is called a head recursion
。的
method savesthe state before jumping into the next recursive call



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/469706.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号