Download presentation
Presentation is loading. Please wait.
Published byÁἌλκιμος Ζωγράφου Modified over 5 years ago
1
Lecture 12 Recursion part 1 CSE 1322 4/26/2018
2
Recursion A recursive method is a method that calls itself.
A recursive method is capable of solving only the base case(s). Each method call divides the problem into two conceptual pieces: a piece that the method knows how to do and a recursive call, or recursion step that solves a smaller problem. A sequence of returns ensues until the original method call returns the result to the caller. 4/26/2018
3
Activation Stack We use the concept of the activation stack to keep track of what memory and instructions "live" inside of each method. When a method is invoked, a new frame for it is placed atop the activation stack. When a method is finished, the frame is removed and control is returned to the next frame down. 4/26/2018
4
Recursive Technique Design
First: Determine the base case, ie, what is the stopping point for the recursion. It should normally be the simplest case. Second: What is the case that is just one step above it? Can it be generalized enough to fit? 4/26/2018
5
Recursive Factorial Calculations
A recursive declaration of the factorial method is arrived at by observing the following relationship: n! = n . (n – 1)! What is the simplest case/ terminating state? you could use either 0! =1 or 1!=1 so … 4/26/2018
6
base case / stopping state
public int factorial( int n) { if(n==1) return 1; … 4/26/2018
7
What if n == 2 2! = 2 *1! which leads to the rest of the code
return n* factorial(n-1); 4/26/2018
8
A recursive factorial method
public int factorial( int n) { if(n==1) return 1; return n* factorial(n-1); } 4/26/2018
9
5! 4/26/2018
10
A Recursive Factorial Program in Java
4/26/2018
11
Common Programming Error
Either omitting the base case or writing the recursion step incorrectly so that it does not converge on the base case will cause infinite recursion, eventually exhausting memory. This error is analogous to the problem of an infinite loop in an iterative (non-recursive) solution. 4/26/2018
12
Stack Operation for Recursive Call
4/26/2018
13
Stack Overflow Avoid stack overflow. Stack overflow occurs when you recurse too many times and run out of memory. Often, it is more efficient to perform calculations via iteration (looping), but it can also be easier to express an algorithm via recursion. Recursion is especially useful for non-linear situations 4/26/2018
14
Challenge Write the recursive process for the following: x^y
You may assume that x and y are both positive integers 4/26/2018
15
x^y solution public long power( int x, int y) { if ( y ==1) return x;
return x*power(x, y-1); } 4/26/2018
16
x^y solution with a Java Program
4/26/2018
17
x^y solution with a C# Program
4/26/2018
18
What is generated by PrintNumbers(30)?
public void PrintNumbers(int n) { if (n > 0) Console.Write(n + " "); PrintNumbers(n - 1); } public void PrintNumbers(int n) { if (n > 0) System.out.println(n + " "); PrintNumbers(n - 1); } 4/26/2018
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.