Results 1 to 8 of 8
Thread: Iteration to Recursion
 06222011, 01:28 AM #1Member
 Join Date
 Feb 2011
 Posts
 78
 Rep Power
 0
Iteration to Recursion
Is there a general rule that can be followed to go from an iterative method to one that is recursive?
I need help transforming the following method to a recursive one
Java Code:void Iteration(int n) { *** int i; *** i = n; *** while ( f(i) == TRUE ) { /*some code that does not change i*/ ** **** i = g(i); *** } }
Java Code:void Recursion(int n) { if ( f(i) == TRUE) { /*some code that does not change i*/ Recursion(g(i)); } }
 06222011, 01:39 AM #2
No there is not a single general rule. Different recursive methods do different things so there is no way you can apply the same rule to all of them. Just as an example Factorial makes a single recursive call while Fibonacci makes 2 recursive calls. How is a single rule supposed to apply to those situations? About the only common thing that can be applied to recursion is: determine what your base case is. That is when do you stop making recursive calls.
 06222011, 01:49 AM #3Member
 Join Date
 Feb 2011
 Posts
 78
 Rep Power
 0
do you think I am on the right track with this one?
 06222011, 02:02 AM #4
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 10
Nitpick: not necessarily, it is more than possible to find the nth fibonnaci number with one recursive call via an iterative recursive process rather than tree recursive.
Java Code:public int fibb(int count, int a, int b){ if(count == 0) return a; else{ return fibb((count  1), (a + b), a); } } public int fibb(int n){ return fibb(n, 1, 0); }
@op: junky is right, there is no rule to converting an iterative approach to a recursive one, especially since there can also be multiple recursive approaches, in my snippet above it's an alternate way to compute fibb numbers to the one junky mentioned.
I wrote a small blog on recursion if you are interested, here is the link:
http://www.javaforums.org/blogs/sun...recursion.html
 06222011, 02:31 AM #5Member
 Join Date
 Feb 2011
 Posts
 78
 Rep Power
 0
with my particular problem, I'm not seeing how it can be implemented recursively, since we need a base case?
 06222011, 02:35 AM #6
You do have a base case. As soon as f(i) returns false the recursive calls stop and it goes back up the call stack (or is that down?).
 06222011, 02:42 AM #7Member
 Join Date
 Feb 2011
 Posts
 78
 Rep Power
 0
so this would seem logical?
Java Code:void Recursion(int n) int i = n; { if ( f(i) == TRUE) { /*some code that does not change i*/ Recursion(g(i)); } }
 06222011, 02:58 AM #8
Similar Threads

Real life example of iteration
By fam2315 in forum New To JavaReplies: 1Last Post: 06192011, 08:33 PM 
Two Dimensional Array Iteration Help
By Stamoulohta in forum New To JavaReplies: 5Last Post: 03132011, 02:39 PM 
Double Iteration Over a Hashmap
By Gusy in forum New To JavaReplies: 1Last Post: 12012010, 02:01 AM 
recursion and tailrecursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12282009, 07:26 PM 
Enum Iteration
By A.Russell in forum New To JavaReplies: 1Last Post: 08152007, 12:17 PM
Bookmarks