recursion

18
1 Recursion Recursive definitions Recursive methods Run-time stack & activation records => Read section 2.3

Upload: adrienne-burke

Post on 30-Dec-2015

25 views

Category:

Documents


1 download

DESCRIPTION

Recursion. Recursive definitions Recursive methods Run-time stack & activation records => Read section 2.3. Recursion is a math and programming tool Technically, not necessary Wasn’t available in early programming languages Advantages of recursion - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Recursion

1

Recursion

Recursive definitions

Recursive methods

Run-time stack & activation records

=> Read section 2.3

Page 2: Recursion

2

Recursion is a math and programming tool Technically, not necessary

Wasn’t available in early programming languages

Advantages of recursion Some things are very easy to do with it, but difficult to do without it

Frequently results in very short programs/algorithms

Disadvantages of recursion Somewhat difficult to understand at first

Often times less efficient than non-recursive counterparts

Presents new opportunities for errors and misunderstanding

Tempting to use, even when not necessary

Recommendation – use with caution, and only if helpful

Page 3: Recursion

3

Factorial – Non-recursive Definition:

N! = N * (N-1) * (N-2) * … * 2 * 1

*Note that a corresponding Java program is easy to write

public static int fact(int n)

:

Recursive DefinitionsRecursive Definitions

Page 4: Recursion

4

Factorial - Recursive Definition:

1 if N=1 Basis Case

N * (N-1)! if N>=2 Recursive Case

Why is it called recursive?

Why do we need a basis case?

Note that the “recursive reference” is always on a smaller value.

N! = {

Recursive DefinitionsRecursive Definitions

Page 5: Recursion

5

Fibonacci - Non-Recursive Definition:

0 1 1 2 3 5 8 13 21 34 …

*Note that a corresponding Java program is easy to write…or is it?

public static int fib(int n)

:

Recursive DefinitionsRecursive Definitions

Page 6: Recursion

6

Fibonacci - Recursive Definition:

0 if N=1 Basis Case

1 if N=2 Basis Case

fib(N-1) + fib(N-2)if N>=3 Recursive Case

Note there are two basis cases and two recursive references.

fib(N) = {

Recursive DefinitionsRecursive Definitions

Page 7: Recursion

7

Printing N Blank Lines – Non-Recursive:

public static void NBlankLines(int n) {

for (int i=1; i<=n; i++)

System.out.println();

}

Recursive Java ProgramsRecursive Java Programs

Page 8: Recursion

8

Printing N Blank Lines – Recursive:

// NBlankLines outputs n blank lines, for n>=0

public static void NBlankLines(int n) {

if (n <= 0) Basis Case

return;

else {

System.out.println();

NBlankLines(n-1); Recursive Case

}

}

*Don’t ever write it this way; this is a simple, first example of recursion.

Recursive Java ProgramsRecursive Java Programs

Page 9: Recursion

9

Another Equivalent Version (slightly restructured):

// NBlankLines outputs n blank lines, for n>=0

public static void NBlankLines(int n) {

if (n > 0) {

System.out.println();

NBlankLines(n-1);

}

}

Recursive Java ProgramsRecursive Java Programs

Page 10: Recursion

10

public static void main(String[] args) {

:

NBlankLines(3);

:

}

public static void NBlankLines(int n) { n=3

if (n > 0) {

System.out.println();

NBlankLines(n-1);

}

}

public static void NBlankLines(int n) { n=2

if (n > 0) {

System.out.println();

NBlankLines(n-1);

}

}

public static void NBlankLines(int n) { n=1

if (n > 0) {

System.out.println();

NBlankLines(n-1);

}

}

public static void NBlankLines(int n) { n=0

if (n > 0) {

System.out.println();

NBlankLines(n-1);

}

}

Recursive Java ProgramsRecursive Java Programs

Page 11: Recursion

11

A Similar Method:

public static void TwoNBlankLines(int n) {

if (n > 0) {

System.out.println();

TwoNBlankLines(n-1);

System.out.println();

}

}

Recursive Java ProgramsRecursive Java Programs

Page 12: Recursion

12

public static void main(String[] args) {

:

TwoNBlankLines(2);

:

}

public static void TwoNBlankLines(int n) {n=2

if (n > 0) {

System.out.println();

TwoNBlankLines(n-1);

System.out.println();

}

}

public static void TwoNBlankLines(int n) {n=1

if (n > 0) {

System.out.println();

TwoNBlankLines(n-1);

System.out.println();

}

}

public static void TwoNBlankLines(int n) {n=0

if (n > 0) {

System.out.println();

TwoNBlankLines(n-1);

System.out.println();

}

}

Recursive Java ProgramsRecursive Java Programs

Page 13: Recursion

13

Are the Following Methods the Same or Different?

public static void TwoNBlankLines(int n) {

if (n > 0) {

System.out.println();

System.out.println();

TwoNBlankLines(n-1);

}

}

public static void TwoNBlankLines(int n) {

if (n > 0) {

TwoNBlankLines(n-1);

System.out.println();

System.out.println();

}

}

Page 14: Recursion

14

Recursive Factorial Definition:

1 if N=1 Basis Case

N * (N-1)! if N>=2 Recursive Case

Recursive Factorial Program:

public static int fact (int n) {

if (n==1)

return 1; Basis Case

else {

int x; Recursive Case

x = fact (n-1);

return x*n;

}

}

N! ={

Page 15: Recursion

15

Another Version:

public static int fact (int n) {

if (n==1)

return 1; Basis Case

else

return n*fact (n-1); Recursive Case

}

Page 16: Recursion

16

Recursive Fibonacci Definition:

0 if N=1 Basis Case

1 if N=2 Basis Case

fib(N-1) + fib(N-2) if N>=3 Recursive Case

Recursive Fibonacci Program:

public static int fib (int n) {

if (n==1)

return 0; Basis Case

else if (n==2)

return 1; Basis Case

else {

int x,y; Recursive Case

x = fib (n-1);

y = fib (n-2);

return x+y;

}

}

fib(N) ={

Page 17: Recursion

17

Another Version:

public static int fib (int n) {

if (n==1)

return 0;

else if (n==2)

return 1;

else

return fib(n-1) + fib(n-2);

}

Page 18: Recursion

18

How does recursion related to stack frames and the run time stack? Note that stack frames are sometimes called allocation

records or activation records

Why might a recursive program be less efficient than non-recursive counterpart?

Why is the recursive fibonnaci function especially inefficient?

Recursion & the Run-time StackRecursion & the Run-time Stack