/
Lecture 12 Recursion part 1 CSE 1322 4/26/2018 12- 1 Recursion Lecture 12 Recursion part 1 CSE 1322 4/26/2018 12- 1 Recursion

Lecture 12 Recursion part 1 CSE 1322 4/26/2018 12- 1 Recursion - PowerPoint Presentation

mitsue-stanley
mitsue-stanley . @mitsue-stanley
Follow
343 views
Uploaded On 2019-11-01

Lecture 12 Recursion part 1 CSE 1322 4/26/2018 12- 1 Recursion - PPT Presentation

Lecture 12 Recursion part 1 CSE 1322 4262018 12 1 Recursion A recursive method is a method that calls itself A recursive method is capable of solving only the base cases Each method call divides the problem into two conceptual pieces a piece that the method knows how to do and a ID: 761744

recursive 201812 factorial method 201812 recursive method factorial int recursion case stack return base public printnumbers call solution frame

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Lecture 12 Recursion part 1 CSE 1322 4/2..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Lecture 12 Recursion part 1 CSE 1322 4/26/2018 12- 1

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/201812- 2

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/201812-3

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/201812-4

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!=1so …4/26/201812-5

base case / stopping state public int factorial( int n) {if(n==1)return 1;…4/26/201812-6

What if n == 2 2! = 2 *1!which leads to the rest of the code return n* factorial(n-1);4/26/201812-7

A recursive factorial method public int factorial( int n) { if(n==1) return 1;return n* factorial(n-1);}4/26/201812-8

5! 4/26/2018 12-9

4/26/201812- 10A Recursive Factorial Program in Java

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/201812-11

Stack Operation for Recursive Call 4/26/201812-12

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/201812-13

Challenge Write the recursive process for the following: x^y You may assume that x and y are both positive integers4/26/201812-14

x^y solution public long power( int x, int y) {if ( y ==1) return x;return x*power(x, y-1);}4/26/201812-15

x^y solution with a Java Program 4/26/201812- 16

x^y solution with a C# Program 4/26/201812- 17

What is generated by PrintNumbers(30)? public void PrintNumbers(int n) { if (n > 0) { Console.Write(n + " "); PrintNumbers(n - 1); Console.Write(n + " "); } }public void PrintNumbers(int n) { if (n > 0) { System.out.println(n + " "); PrintNumbers(n - 1); System.out.println(n + " "); } } 4/26/2018 12- 18