Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.

Similar presentations


Presentation on theme: "Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1."— Presentation transcript:

1 Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1

2 The factorial function n! =1 ∙ 2 ∙ 3 ∙ … ∙ (n – 1) ∙ n ?? n! = 1 if n = 0 (base case, anchor) n ∙ (n – 1)!if n > 0 (inductive step) Data Structures and Algorithms in Java, Third EditionCh05 – 2

3 Application of the definition of n! = 3 ∙ 2! = 2 ∙ 1! = 1 ∙ 0! = ? 0! = ? = 1 ∙ 1 = 1 = 2 ∙ 1 = 2 = 3 ∙ 2 = 63! 1! 2! = 1 Data Structures and Algorithms in Java, Third EditionCh05 – 3

4 Implementation of the factorial function long factorial(long n) { if (n == 0) return 1; else return n * factorial(n-1); } Data Structures and Algorithms in Java, Third EditionCh05 – 4

5 Stack frame parameters and local variables return value return address Data Structures and Algorithms in Java, Third EditionCh05 – 5

6 Executing the factorial method long factorial(long n) { if (n == 0) return 1; else return n * factorial(n-1); }............................. void f() {............................. long m = factorial(3);............................. } (20) (10) Data Structures and Algorithms in Java, Third EditionCh05 – 6

7 0 ? (10) 1 ? 2 ? 3 ? (20) 0 1 (10) 1 ? 2 ? 3 ? (20) 1 1 (10) 2 ? 3 ? (20) 2 2 (10) 3 ? (20) * 3 6 1 ? (10) 2 ? 3 ? (20) 2 ? (10) 3 ? (20) 3 ? * * long factorial(long n) { if (n == 0) return 1; else return n * factorial(n-1); }............................. void f() {............................. long m = factorial(3);............................. } (20) (10) Data Structures and Algorithms in Java, Third EditionCh05 – 7

8 Tracing recursion void f(int n) { if (n > 0) { f(n-1); System.out.print(n + " "); f(n-1); } Data Structures and Algorithms in Java, Third EditionCh05 – 8

9 Tracing recursion using indentation f(1) f(0) 1 output: 1 f(3) f(2) f(1) f(0) 1 2 f(1) f(0) 1 3 f(2) f(1) f(0) 1 2 f(1) f(0) 1 output: 1 2 1 3 1 2 1 f(2) f(1) f(0) 1 2 f(1) f(0) 1 output: 1 2 1 void f(int n) { if (n > 0) { f(n-1); System.out.print(n + " "); f(n-1); }

10 Tracing recursion using tree of calls void f(int n) { if (n > 0) { f(n-1); System.out.print(n + " "); f(n-1); } f(1) f(0) 1 f(0) output: 1 f(2) f(1) 2 f(0) 1 f(0) output: 1 2 1 f(2) f(1) 2 f(1) f(0) 1 f(0) output: 1 2 1 3 1 2 1 f(2) f(1) 2 f(1) f(0) 1 f(0) f(3) 3 f(1) f(0) 1 f(0) Data Structures and Algorithms in Java, Third EditionCh05 – 10

11 Excessive recursion 5252 4141 3030 4242 3131 3232 2222 2121 2020 1010 1111 11 2121 1010 1111 11 11 3131 2121 2020 1010 1111 11 1 1 nknk n-1 k-1 n-1 k + = 1 if k = 0 or k = n otherwise

12 Designing recursive methods: example 562-3 ? 2 65 ? + 11 + 13 + 10 Data Structures and Algorithms in Java, Third EditionCh05 – 12

13 int add(int[] a, int last) { if (last == 0) return a[0]; else return add(a,last-1) + a[last]; } int add(int[] a) { // a.length ≥ 1 return add(a,a.length-1); } Designing recursive methods: example (cont’d) Data Structures and Algorithms in Java, Third EditionCh05 – 13


Download ppt "Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1."

Similar presentations


Ads by Google