Results 1 to 3 of 3
  1. #1
    OptimusPrime is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default recursion and tail-recursion differences

    hello,
    i need some tuning about this issue,
    what is the difference in the structure of the recursions when the final result is computed?
    what is the difference in the nature of the “base case” (the case that is handled within the stop condition of the recursion).
    maybe some other differences you can think of...
    thanks,
    OP
    Last edited by OptimusPrime; 12-30-2009 at 07:47 AM.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    the final result usually returns a value whereas the other results return another function call. The base case is used to tell the recursive call when to stop calling itself or else you'll end up in a stack overflow and your loop will never end.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    One (theoretical) important aspect is: if the size of a problem is 'n' (n characters in a String, n nodes in a tree etc.) and a recursive solution needs a stack of size O(n) an iterative solution will be better; so e.g. reversing a String or calculating the n-th Fibonacci number is better done iteratively than recursively. On the other hand traversing a (binary) tree just needs O(log(n)) slots on a stack so it can be better done recursively.

    kind regards.

    Jos

Similar Threads

  1. Recursion
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 04-09-2009, 02:26 AM
  2. Recursion
    By jachandru in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:52 PM
  3. Recursion
    By Mika in forum New To Java
    Replies: 5
    Last Post: 01-04-2009, 01:13 AM
  4. Recursion help
    By rjg_2186 in forum New To Java
    Replies: 1
    Last Post: 01-02-2009, 08:03 AM
  5. Help With Recursion
    By andrew777 in forum New To Java
    Replies: 1
    Last Post: 03-29-2008, 12:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •