recursion!. can a method call another method? yes
Post on 21-Dec-2015
227 views
TRANSCRIPT
Rules:
1. Recursive call must be to a smaller
version of the problem.
2. Recursion must be heading for the
base case.
public static void main(String[] args)
{
int f;
f = fact(4);
System.out.println(f);
}
Call to fact(4)
public static int fact(int 4)
{
if (4 == 0) return 1;
else return 4 *fact(4 -1);
}
public static int fact(int 3)
{
if (3 == 0) return 1;
else return 3 *fact(3 -1);
}
Call to fact(2)
public static int fact(int 4)
{
if (4 == 0) return 1;
else return 4 *fact(4 -1);
}
public static int fact(int 3)
{
if (3 == 0) return 1;
else return 3 *fact(3 -1);
}
public static int fact(int 2)
{
if (2 == 0) return 1;
else return 2 *fact(2 -1);
}
Call to fact(1)
public static int fact(int 4)
{
if (4 == 0) return 1;
else return 4 *fact(4 -1);
}
public static int fact(int 3)
{
if (3 == 0) return 1;
else return 3 *fact(3 -1);
}
public static int fact(int 2)
{
if (2 == 0) return 1;
else return 2 *fact(2 -1);
}
public static int fact(int 1)
{
if (1 == 0) return 1;
else return 1 *fact(1 -1);
}
Call to fact(0)
public static int fact(int 4)
{
if (4 == 0) return 1;
else return 4 *fact(4 -1);
}
public static int fact(int 3)
{
if (3 == 0) return 1;
else return 3 *fact(3 -1);
}
public static int fact(int 2)
{
if (2 == 0) return 1;
else return 2 *fact(2 -1);
}
public static int fact(int 1)
{
if (1 == 0) return 1;
else return 1 *fact(1 -1);
}
public static int fact(int 0)
{
if (0 == 0) return 1;
else return 0 *fact(0 -1);
}
public static int fact(int 4)
{
if (4 == 0) return 1;
else return 4 *fact(4 -1);
}
public static int fact(int 3)
{
if (3 == 0) return 1;
else return 3 *fact(3 -1);
}
public static int fact(int 2)
{
if (2 == 0) return 1;
else return 2 *fact(2 -1);
}
public static int fact(int 1)
{
if (1 == 0) return 1;
else return 1 *fact(1 -1);
}
public static int fact(int 0)
{
if (0 == 0) return 1;
else return 0 *fact(0 -1);
}
public static int fact(int 4)
{
if (4 == 0) return 1;
else return 4 *fact(4 -1);
}
public static int fact(int 3)
{
if (3 == 0) return 1;
else return 3 *fact(3 -1);
}
public static int fact(int 2)
{
if (2 == 0) return 1;
else return 2 *fact(2 -1);
}
public static int fact(int 1)
{
if (1 == 0) return 1;
else return 1 *fact(1 -1);
}
Returned 1
public static int fact(int 4)
{
if (4 == 0) return 1;
else return 4 *fact(4 -1);
}
public static int fact(int 3)
{
if (3 == 0) return 1;
else return 3 *fact(3 -1);
}
public static int fact(int 2)
{
if (2 == 0) return 1;
else return 2 *fact(2 -1);
}
Returned 1
public static int fact(int 4)
{
if (4 == 0) return 1;
else return 4 *fact(4 -1);
}
public static int fact(int 3)
{
if (3 == 0) return 1;
else return 3 *fact(3 -1);
}
Returned 2
Comments about recursion
1. Recursion is never necessary. Can always be done iteratively.
2. Can hide inefficiency.
3. But may be the easiest, clearest, shortest way to write a program.
public static int fib(int n)
{ // pre: n>=0
// post: returned value is fib(n)
if (n==0 || n==1) return n;
else return fib(n-1)+fib(n-2);
}