Questions tagged [recursion]

For questions about recursion, the practice of calling a method or function from within itself.

See here for more information.

166 questions
76
votes
18 answers

In plain English, what is recursion?

The idea of recursion is not very common in real world. So, it seems a bit confusing to the novice programmers. Though, I guess, they become used to the concept gradually. So, what can be a nice explanation for them to grasp the idea easily?
Gulshan
  • 9,442
46
votes
17 answers

Recursion without factorial, Fibonacci numbers etc

Almost every article I can find about recursion includes the examples of factorial or Fibonacci Numbers, which are: Math Useless in real life Are there some interesting non-math code examples to teach recursion? I'm thinking divide-and-conquer…
synapse
  • 399
26
votes
9 answers

How do I explain "Recursion" to an 8-year-old kid?

Possible Duplicate: In plain English, what is recursion? What is the best way to explain "Recursion" to 8 years old kid? I tried with the Fibonacci Series but i failed.
Soner Gönül
  • 575
  • 2
  • 9
  • 29
13
votes
3 answers

Are there advantages for using recursion over iteration - other than sometimes readability and elegance?

I am about to make two assumptions. Please correct me if they're wrong: There isn't a recursive algorithm without an iterative equivalent. Iteration is always cheaper performance-wise than recursion (at least in general purpose languages such as…
Aviv Cohn
  • 21,388
12
votes
7 answers

Recursion -- is it "divide and conquer" or "code reuse"

Recursion -- as we all know -- is one of those problems -- that wrapping your head around feels like achieving a "milestone" in your programming voyage. But when it comes to actually using it in real world problems -- knowing the mechanics of…
treecoder
  • 9,495
7
votes
1 answer

What does "recursive" mean for a program which processes files and directories?

I know what recursion is in programming. I do understand the basics of version control systems (have used svn that is). But I have often wondered what the meaning of "recursion" or "recursive" is with respect to version control systems. What does…
minusSeven
  • 723
  • 1
  • 7
  • 17
5
votes
1 answer

Best way to deal with Floors and Ceiling when using substitution method to solve Recurrences

I'm currently using substitution method to solve recurrences. The problem I'm having is dealing with T(n) that have either ceilings or floors. For example in the following example see example here. They end up using the guess: T(n) ≧ c(n+2)…
3
votes
5 answers

Advantage or disadvantages between recursive functions and for-loops

Whats the difference between the two procedures? When should I be using a recursive function instead of a normal for-loop?
3
votes
3 answers

How to mentally keep track of recursion

I have great trouble doing recursion, especially trying to visualize it. A great example is the aging wine problem described in the top answer of this link:…
mrQWERTY
  • 243
2
votes
2 answers

Why left recursion not looping?

I'm trying to read the parser of PHP, that says: top_statement_list: top_statement_list top_statement | /* empty */ ; It is a left-recursion, but why it don't looping infinitely? My question is because I'm trying building a parser…
2
votes
2 answers

Is this a good example of open recursion?

I understand open recursion as the process of a method on a class calling another method on a class using a keyword such as this, but whereby the method call may actually be bound to a sub class at run time. Is this a fair demonstration of open…
Fenton
  • 1,069
1
vote
2 answers

How to decide how to build a recursive function

Sometimes when you're coding you're on a problem which can be solved with some recursive method. What is for you the best way to detect when the recursion is a good way to solve a problem and how to implement it efficiently? I mean, how do you…
toto
  • 21
1
vote
1 answer

Why does this recursion method work? I have explored it for a day or two, and I cannot figure out why

Problem Statement: I have a tree with node values ( i , j ) where i , j < 1. The children of each node take on the values (i - 1, j), (i - 1, j - 1), and (i, j - 1) respectively. Now, i and j have constraints where they cannot be less than zero, so,…
0
votes
1 answer

Is there a name for this technique of simplifying recursion code by passing some logic from the caller to the callee?

The technique involves moving some logic from the caller to the callee. Usually the logic that is moved is a check. Here's an example of the technique applied to code that solves the "Construct BST (Binary Search Tree) from the Preorder Traversal…
joseville
  • 123
0
votes
1 answer

Recursion explanation

I am not able to understand this basic recursion example. When I executed this code. The output was 43210. Should not the output be 44444, as the value of num is updated to 4 to the latest recursive call or how is the value of num getting…
penta
  • 135
1
2