递归的相关性

发布于 2024-11-04 13:59:33 字数 168 浏览 1 评论 0 原文

所以我目前正在尝试掌握递归的概念,并且我理解我遇到的大部分问题,但我觉得它的使用不适用于太多的计算问题。但这只是新手的假设,所以我想问,作为程序员,递归有很多实际用途吗?另外,它可以解决哪些典型问题?我见过的唯一的问题是堆排序和脑筋急转弯类型的问题,例如“河内塔”,它看起来非常具体且缺乏广泛的用途。

谢谢

So I'm currently trying to grasp the concept of recursion, and I understand most of the problems that I've encountered, but I feel as though its use wouldn't be applicable to too many computing issues. This is just a novice's assumption though, so I'm asking, are there many practical uses for recursion as a programmer? And also, what typical problems can be solved with it? The only ones that I've seen are heap sort and brain teaser-type problems like "The Towers of Hanoi which just seems very specific and lacking broad use.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

安静被遗忘 2024-11-11 13:59:33

递归在编程中有很多用途 - 一个典型的例子是导航树结构,您可以在发现的每个子元素的情况下调用导航函数等。

There are a plethora of uses for recursion in programming - a classic example being navigating a tree structure, where you'd call the navigation function with each child element discovered, etc.

月朦胧 2024-11-11 13:59:33

以下是一些如果没有递归几乎不可能实现的领域:

  • XML、HTML 或任何其他树状文档结构
  • 编译和解析
  • 自然语言处理
  • 分而治之算法
  • 许多数学概念,例如阶乘

递归可以为原本复杂的问题带来极其优雅的解决方案。如果您对编程作为一门艺术感兴趣,那么您确实应该更深入地研究。

哦,如果您不确定,这是递归的可靠定义:

递归(名词):参见“递归”

Here are some fields which would be almost impossible without recursion:

  • XML, HTML or any other tree like document structure
  • Compilation and parsing
  • Natural Language Processing
  • Divide and conquer algorithms
  • Many mathematical concepts, e.g. factorials

Recursion can lead to brilliantly elegant solutions to otherwise complex problems. If you're at all interested in programming as an art, you really should delve deeper.

Oh and if you're not sure, here's a solid definition of recursion:

Recursion (noun): See "Recursion"

掐死时间 2024-11-11 13:59:33

我想这取决于你要做什么。作为从事企业 Web 工作的 C#/ASP.NET 开发人员,我每年编写的递归函数可能还不到一个。当我摆弄我的爱好代码(主要是统计研究)时,我发现有更多应用递归的机会。部分原因是主题,部分原因是我更依赖客户在进行公司工作时已经决定的第三方库(其中实现了需要递归的算法)。

It depends on what you're going to be doing I suppose. I probably write less than one recursive function a year as a C#/ASP.NET developer doing corporate web work. When I'm screwing around with my hobby code (mostly stat research) I find a lot more opportunities to apply recursion. Part of this is subject matter, part of it is that I'm much more reliant on 3rd party libraries that the client has already decided on when doing corporate work (where the algorithms needing recursion are implemented).

只为守护你 2024-11-11 13:59:33

它不是你每天都使用的东西。但许多有关数据搜索和排序的算法都可以利用它。一般来说,大多数递归算法也可以使用迭代来编写;通常,递归版本更简单。

It's not something you use every day. But many algorithms about searching and sorting data can make use of it. In general, most recursive algorithms can also be written using iteration; oftentimes the recursive version is simpler.

↙温凉少女 2024-11-11 13:59:33

如果您检查与此问题“相关”列出的问题,您会发现有关递归的“过多”内容,这将帮助您更好地理解它。

递归并不是什么新鲜事,也不仅仅是一个玩具概念。递归算法早在计算机出现之前就已经存在了。

“阶乘”的经典定义就是一个很好的例子:

fact(x) =
if x < 0 then fact(x) is undefined
if x = 0 then fact(0) = 1
if x > 0 then fact(x) = x * fact(x-1)

这不是由认为递归是一个很酷的玩具的计算机极客创建的。这是标准的数学定义。

If you check the questions which are listed as "Related" to this question, you will find a "plethora" of stuff about recursion that will help you to understand it better.

Recursion isn't something new, and it is not just a toy concept. Recursive algorithms have been around since before there were computers.

The classic definition of "factorial" is a prime example:

fact(x) =
if x < 0 then fact(x) is undefined
if x = 0 then fact(0) = 1
if x > 0 then fact(x) = x * fact(x-1)

This isn't something that was created by computer geeks who thought that recursion was a cool toy. This is the standard mathematical definition.

荭秂 2024-11-11 13:59:33

调用递归作为一种程序构造,几乎不应该使用,除非在非常高级的语言中,您希望编译器将其优化为不同的构造。使用调用递归,除非您可以在深度上建立较小的界限,否则会导致堆栈溢出,而不是为您解答问题的良好堆栈溢出。 :-)

另一方面,递归作为一个算法概念非常有用。它对于处理任何递归定义的数据格式(例如 HTML 或 XML,或分层文件系统)以及在搜索、排序和(每个人最喜欢的)图形渲染等无数其他领域中实现重要算法至关重要。

Call recursion, as a program construct, is something that should almost never be used except in extremely high-level languages where you expect the compiler to optimize it to a different construct. Use of call recursion, except when you can establish small bounds on the depth, leads to stack overflow, and not the good kind of Stack Overflow that answers your questions for you. :-)

Recursion as an algorithmic concept, on the other hand, is very useful. It's key to working with any recursively-defined data formats (like HTML or XML, or a hierarchical filesystem) as well as for implementing important algorithms in searching, sorting, and (everyone's favorite) graphics rendering, among countless other fields.

任性一次 2024-11-11 13:59:33

有几种语言不支持循环(即 for 和 while),因此当您需要重复行为时,您需要使用递归(我相信 J 没有循环)。在许多示例中,递归需要少的代码。比如我写了一个isPrime方法,只用了两行代码。

public static boolean isPrime(int n)
{
返回 n!=1&&isPrime(n,2);
}

public static boolean isPrime(int n,int c)
{
返回c==n||n%c!=0&&isPrime(n,c+1);
另一个

迭代解决方案将需要更多代码:

public static boolean isPrime(int n)
{
    if(n==1) return false;
    int c=2;
    while(c!=n)
    {
        if(n%c==0) return false;
    }
    return true;
}

很好的例子是当您使用 ListNodes 时,例如,如果您想检查 ListNode 中的所有元素是否相同,则递归解决方案会花费更多更轻松。

public static <E> boolean allSame(ListNode<E> list)
    {
        return list.getNext()==null||list.getValue().equals(list.getNext().getValue())&&allSame(list.getNext());
    }

迭代解决方案看起来像这样:

public static <E> boolean allSame(ListNode<E> list)
{
    while(list.getNext()!=null)
    {
        if(!list.getValue().equals(list)) return false;
        list=list.getNext();
    }
    return true;
}

正如您所看到的,在大多数情况下,递归解决方案比迭代解决方案更短。

There are are several languages that don't support loops (ie. for and while), and as a result when you need repeating behavior, you need to use recursion(I believe that J does not have loops). In many examples, recursion requires much less code. For example, I wrote an isPrime method, it took only two lines of code.

public static boolean isPrime(int n)
{
return n!=1&&isPrime(n,2);
}

public static boolean isPrime(int n,int c)
{
return c==n||n%c!=0&&isPrime(n,c+1);
}

The iterative solution would take much more code:

public static boolean isPrime(int n)
{
    if(n==1) return false;
    int c=2;
    while(c!=n)
    {
        if(n%c==0) return false;
    }
    return true;
}

Another good example is when you are working with ListNodes, for example if you would like to check if all the elements in a ListNode are the same, a recursive solution would be much easier.

public static <E> boolean allSame(ListNode<E> list)
    {
        return list.getNext()==null||list.getValue().equals(list.getNext().getValue())&&allSame(list.getNext());
    }

The iterative solution would look something like this:

public static <E> boolean allSame(ListNode<E> list)
{
    while(list.getNext()!=null)
    {
        if(!list.getValue().equals(list)) return false;
        list=list.getNext();
    }
    return true;
}

As you can see, in most cases recursive solutions are shorter than iterative solutions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文