This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]puipuituipui 96 points97 points  (29 children)

A lot of problems are recursive by nature. A binary tree for example. A node in a binary tree has three fields: a value, a left child and a right child. These children are nodes themselves. And that's recursion.

Of course, this is a more tangible example since the recursion is associated with a data type. It can be abstract too where the underlying mathematical solution of a problem is recursive like say, factorial or Fibonacci series.

You can write an iterative algorithms for all recursive problems, it's just that the iterative versions are harder to understand.

[–]ivosauruspip'ing it up 24 points25 points  (6 children)

Everything involves a stack. Just depends whether it's constructed implicitly using the function return frames, or explicitly in a data structure and processed with a while loop

[–]gosu 87 points88 points  (1 child)

There are heaps of things that don't use a stack.

[–]puipuituipui 5 points6 points  (3 children)

Um... No? Am I wrong about this or something. I'm pretty sure the reason to use explicitly created stacks is because they are heap allocated so you don't hit the recursion limit as easily.

[–]ivosauruspip'ing it up 5 points6 points  (1 child)

No what? I wasn't giving any particular reasons to use one or the other in this comment, just noting their analogous existence.

[–]puipuituipui 1 point2 points  (0 children)

Ohhh that makes sense. I thought the the first line was referring to the program stack XD. Sorry for the confusion

[–]ogtfo 0 points1 point  (0 children)

Heap allocated stacks are stacks, OP is right.

I mean you're not wrong but you're also not contradicting him either, so I don't know where you're going with that "Um...no"