Thread: Problems: When recursion is best

  1. #1
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531

    Problems: When recursion is best

    Can anyone provide problems when recursions are the best solutions? Tower of Hanoi is an example.

    Any site you know.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  2. #2
    Unregistered
    Guest
    Hav u got the solution to Towers of Hanoi? If possible pls post it or send to me directly at [email protected]

    many thnx

  3. #3
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Yeah.. I have already done that and posted to another thread.

    I guess you have already got a copy through your mail.

    visit the page http://www.mazeworks.com/hanoi/index.htm
    I'm sure you will get the graphical idea to imagine how it works.
    Last edited by zahid; 01-07-2002 at 12:09 AM.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    If you want to see an instance in which recursion is so much easier and clearer than iteration, look into writing your own binary tree data structure.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I have no idea what it is........forgive me.

  6. #6
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Originally posted by SilentStrike
    If you want to see an instance in which recursion is so much easier and clearer than iteration, look into writing your own binary tree data structure.
    yeah.. I have one.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    You have an NxN square, seperated into NxN cells. Find the number of paths from the bottom left cell to the top right cell of the square.

    You have two islands. One island has 3 pirates, 3 cannibals, and a boat. The other island is empty. Here are the rules...
    1. If there are more cannibals than pirates on an island, then the cannibals eat the pirates, and you lose. (Note that if there are no pirates on an island, then this rule doesn't apply to that island)
    2. The only way to move from one island to the other island is the boat.
    3. The boat can carry at most 2 people.
    4. The boat must carry at least 1 person (to sail it).
    Write a program to find a way to get all the cannibals and pirates from one island to the other.
    Code:
    // Since the problem is writing a program, here's a sample solution
    PPPCCCB -  // B = Boat
    PPCC - BPC // P = Pirate
    PPPCCB - C // C = Cannibal
    PPP - BCCC
    PPPCB - CC
    PC - BPPCC
    PPCCB - PC
    CC - BPPPC
    CCCB - PPP
    C - BPPPCC
    CCB - PPPC
     - BPPPCCC
    Towers of Hanoi
    Finding the nth element, from the union of two sorted sets
    Quicksort
    Building a 2-3Tree
    Traversing a binary tree
    Fractals
    Callou collei we'll code the way
    Of prime numbers and pings!

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Recursion is best when:

    You have plenty of stack space.
    It's faster than not using it.
    When working with fractals (Terrain generation comes to mind- midpoint displacement algo, etc.)

    I always try to get it iterative first. If that is too ugly or is too slow (or too complicated) then I use recursion.

    There are several posts here about how to solve the Hanoi problem. You may have to search through the posts, but they are here.

  9. #9
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Thanks QuestionC.
    More problems will help to master the logic.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. recursion project
    By Psycho in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2002, 03:24 PM
  3. When to use recursion ?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-06-2002, 10:36 PM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM
  5. Recursion function won't exit
    By cheesehead in forum C++ Programming
    Replies: 20
    Last Post: 10-30-2001, 03:58 PM