Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Iteration vs. Recursion

Name: Anonymous 2008-08-04 16:54

Iteration obviously, unless your algorithm can't possibly be implemented any other way.

Name: Anonymous 2008-08-06 9:51

...

An iterative solution is often completely different from the recursive solution. >>15 is a shitty iterative prefix tree traversal -- the recursive variant looks like this:

def traverse( node, func ):
    if not func: return
    func( node )
    traverse( node.left, func )
    traverse( node.right, func )


It doesn't use tail recursion. It can't use tail recursion. In order to avoid raping the fuck out of your stack, you must use an iterative approach like the one above.

Tail recursion is the same goddamn thing as an iterative approach -- any tail recursive function can be wrappered into a loop which does the same thing (and this is what any self-respecting compiler does for you), and any iterative loop can be restructured into a tail-recursive function. A true recursive function (qsort, tree traversal, etc) is such that it is not tail recursive, but rather, branches out.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List