Top Banner
Recursion!
32

Recursion!. Can a method call another method? YES.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Recursion!. Can a method call another method? YES.

Recursion!

Page 2: Recursion!. Can a method call another method? YES.

Can a method call another

method?

Page 3: Recursion!. Can a method call another method? YES.

Can a method call another

method?YES

Page 4: Recursion!. Can a method call another method? YES.

Can a method itself?

Also YES

Page 5: Recursion!. Can a method call another method? YES.
Page 6: Recursion!. Can a method call another method? YES.

Rules:

1. Recursive call must be to a smaller

version of the problem.

2. Recursion must be heading for the

base case.

Page 7: Recursion!. Can a method call another method? YES.

public static int fact(int n)

{

if (n == 0) return 1;

else return n*fact(n-1);

}

Page 8: Recursion!. Can a method call another method? YES.

public static void main(String[] args)

{

int f;

f = fact(4);

System.out.println(f);

}

Call to fact(4)

Page 9: Recursion!. Can a method call another method? YES.

public static int fact(int n)

{

if (n == 0) return 1;

else return n *fact(n -1);

}

Page 10: Recursion!. Can a method call another method? YES.

public static int fact(int 4)

{

if (4 == 0) return 1;

else return 4 *fact(4 -1);

}

Call to fact(3)

Page 11: Recursion!. Can a method call another method? YES.

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)

Page 12: Recursion!. Can a method call another method? YES.

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)

Page 13: Recursion!. Can a method call another method? YES.

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)

Page 14: Recursion!. Can a method call another method? YES.

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);

}

Page 15: Recursion!. Can a method call another method? YES.

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);

}

Page 16: Recursion!. Can a method call another method? YES.

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

Page 17: Recursion!. Can a method call another method? YES.

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

Page 18: Recursion!. Can a method call another method? YES.

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

Page 19: Recursion!. Can a method call another method? YES.

public static int fact(int 4)

{

if (4 == 0) return 1;

else return 4 *fact(4 -1);

}

Returned 6

Page 20: Recursion!. Can a method call another method? YES.

public static void main(String[] args)

{

int f;

f = fact(4);

System.out.println(f);

}

Returned 24

Page 21: Recursion!. Can a method call another method? YES.

public static void main(String[] args)

{

int f;

f = fact(4);

System.out.println(f);

}

Displays 24

Page 22: Recursion!. Can a method call another method? YES.

?

Page 23: Recursion!. Can a method call another method? YES.

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.

Page 24: Recursion!. Can a method call another method? YES.

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);

}

Page 25: Recursion!. Can a method call another method? YES.

Fib(8)

Page 26: Recursion!. Can a method call another method? YES.

Fib(8)

Fib(6)Fib(7)

Page 27: Recursion!. Can a method call another method? YES.

Fib(8)

Fib(6)

Fib(5)Fib(6)

Fib(7)

Page 28: Recursion!. Can a method call another method? YES.

Fib(8)

Fib(4)Fib(5)

Fib(6)

Fib(5)

Fib(5)

Fib(6)

Fib(7)

Fib(4)

Page 29: Recursion!. Can a method call another method? YES.

Yadda yadda yadda

Page 30: Recursion!. Can a method call another method? YES.

Fib(8)

Fib(4)Fib(5)

Fib(6)

Fib(5)

Fib(5)

Fib(6)

Fib(7)

Fib(2)Fib(3)Fib(3)Fib(4)Fib(3)Fib(4)Fib(4)

Page 31: Recursion!. Can a method call another method? YES.

Fib(8)

Fib(4)Fib(5)

Fib(6)

Fib(5)

Fib(5)

Fib(6)

Fib(7)

Fib(2)Fib(3)Fib(3)Fib(4)Fib(3)Fib(4)Fib(4)

Page 32: Recursion!. Can a method call another method? YES.

To compute fib(8), we call:

fib(8) 1 time

fib(7) 1 times

fib(6) 2 times

fib(5) 3 times

fib(4) 5 times

fib(3) 8 times

fib(2) 13 times

fib(1) 21 times

fib(0) 13 times

A total of 67 method calls!