“Necessary” Uses of Recursion in Imperative Languages

后端 未结 9 1032
独厮守ぢ
独厮守ぢ 2020-12-17 17:10

I\'ve recently seen in a couple of different places comments along the lines of, \"I learned about recursion in school, but have never used it or felt the need for it since

相关标签:
9条回答
  • 2020-12-17 17:37

    In my work recursion is very rarely used for anything algorithmic. Things like factorials etc are solved much more readably (and efficiently) using simple loops. When it does show up it is usually because you are processing some data that is recursive in nature. For example, the nodes on a tree structure could be processed recursively.

    If you were to write a program to walk the nodes of a binary tree for example, you could write a function that processed one node, and called itself to process each of it's children. This would be more effective than trying to maintain all the different states for each child node as you looped through them.

    0 讨论(0)
  • 2020-12-17 17:39

    I have a List of reports. I am using indexers on my class that contains this list. The reports are retrieved by their screen names using the indexers. In the indexer, if the report for that screen name doesn't exist it loads the report and recursively calls itself.

    public class ReportDictionary
        {
            private static List<Report> _reportList = null;
    
            public ReportColumnList this[string screenName]
            {
                get
                {
                    Report rc = _reportList.Find(delegate(Report obj) { return obj.ReportName == screenName; });
    
                    if (rc == null)
                    {
                        this.Load(screenName);
                        return this[screenName]; // Recursive call
                    }
                    else
                        return rc.ReportColumnList.Copy();
                }
                private set
                {
                    this.Add(screenName, value);
                }
            }
    
        }
    

    This can be done without recursion using some additional lines of code.

    0 讨论(0)
  • 2020-12-17 17:41

    The most well-known example is probably the quicksort algorithm developed by by C.A.R. Hoare.

    Another example is traversing a directory tree for finding a file.

    0 讨论(0)
  • 2020-12-17 17:50

    When you are walking any kind of tree structure, for example

    • parsing a grammar using a recursive-descent parser
    • walking a DOM tree (e.g. parsed HTML or XML)

    also, every toString() method that calls the toString() of the object members can be considered recursive, too. All object serializing algorithms are recursive.

    0 讨论(0)
  • 2020-12-17 17:50

    Recursion can always be rewritten as iteration with an external stack. However if you're sure that you don't risk very deep recursion that would lead to stackoverflow, recursion is a very convenient thing.

    One good example is traversing a directory structure on a known operating system. You usually know how deep it can be (maximum path length is limited) and therefore will not have a stackoverflow. Doing the same via iteration with an external stack is not so convenient.

    0 讨论(0)
  • 2020-12-17 17:51

    There are no "necessary" uses of recursion. All recursive algorithms can be converted to iterative ones. I seem to recall a stack being necessary, but I can't recall the exact construction off the top of my head.

    Practically speaking, if you're not using recursion for the following (even in imperative languages) you're a little mad:

    1. Tree traversal
    2. Graphs
    3. Lexing/Parsing
    4. Sorting
    0 讨论(0)
提交回复
热议问题