Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion DRILL: Please take out your notes on Recursion

Similar presentations


Presentation on theme: "Recursion DRILL: Please take out your notes on Recursion"— Presentation transcript:

1 Recursion DRILL: Please take out your notes on Recursion
double Power (int base, int exp) { double ans = 1; for (int j = 0; j < exp; j++) ans = ans * base; return ans; } Procedures: 1. Go over Sorting and Searching Practice 2. Notes on Recursion 3. Recursion Practice 4. Recursion Programs HW : Recursion Ditto Start working on Recursion Problems

2 Objective: You will solve programming problems that contain repetition by defining, analyzing, and completing activities which involve recursion

3 Recursion In Java, any method can call another method. A method can even call When a method calls itself, it is making a recursion : the process of a method calling itself Recursion is a powerful technique that can be used in place of Any recursive method can be rewritten using iteration. itself recursive call iteration (looping).

4 To calculate the result of raising an integer to a positive power, if x is an integer and n is a positive integer, the formula xn is : xn = x * x * x * x * * x n times We could also write this formula as : xn = x * (x * x * x * * x) times or as this is a n - 1 xn = x * xn - 1 recursive definition

5 recursive definition : a definition in which something is defined
in terms of smaller versions of itself. 256= 25 * 25 * 25 * 25* 2 5* 25 = 25 * (25 * 25 * 25 * 25 * 25) = 25 * 255 = 25 * (25 *(25 * 25 * 25 * 25)) = 25 * (25 * 254) = 25 * (25 *(25 * (25 * 25 * 25))) = 25 * (25 * (25 * 253)) = 25 * (25 *(25 * (25 *( 25 *25)))) = 25 * (25 * (25 * (25 * 252))) = 25 * (25 * (25 * (25 *( 25 *(25))))) = 25 * (25 * (25 * (25 * (25 * (251)))

6 All recursive functions must have a
All recursive functions must have a The base case the chain of recursive calls. base case (stopping state): a case that does generate a recursive call. What is the base case in the problem of 256? base case terminates not 250

7 Write Power Recursively
int Power (int base, int exp) { } if ( exp = = 0) // base case return 1; else // recursive call return base * Power(base, exp - 1)

8 How can I figure out what is returned from the following call?
Power (25, 6)

9 5 * (4*(3*(2*1!)))= 5*(4*(3*(2*(1*0!))))
You Try : 1. Express 5! in terms of a smaller definition of itself. 2. What is the base case for Factorial? 3. Write a method called Factorial that uses recursion to determine the factorial of a number. 5*(4!) = 5 *(4*3!) = 5*(4*(3*2!)) = 5 * (4*(3*(2*1!)))= 5*(4*(3*(2*(1*0!)))) 0! n = 0

10 int Factorial (int n) { if (n == 0) return 1; else return n * Factorial (n - 1); }

11 ` What is Happening in Memory
When a function is called (either recursively or non-recursively), the computer system creates temporary storage for the actual parameters and the methods’s local variables in a region of memory called the run-time stack. When the function returns, its parameters and local variables are released from the run-time stack. stack : a dynamic data structure where access can only be made from___________. A stack is a ________ structure - __________________ ` one end LIFO Last In First Out

12 Each call to a function can be thought of as adding a “tray” to the run-time stack.
Suppose there is a call to Factorial in main. ans = Factorial (5);

13 1 1 1 2 2 3 6 4 24 5 120 int Factorial (int n) { if (n == 0) return 1;
else return n * Factorial (n - 1); } 1 1 1 2 2 3 6 4 24 5 120

14 There is only a finite amount of space on the run-time stack
There is only a finite amount of space on the run-time stack. If the function never returns a value, eventually all the memory space on the stack is used and the program crashes with an error message such as “ ”. A function returns when there is a or when a void function reaches the run-time stack overflow return statement closing brace

15 int Recur (int num) { if (num ==0) return 0; else return num + Recur(num-1); } What is returned from the call Recur (4)?

16 return num + Recur(num-1); }
int Recur (int num) { if (num ==0) return 0; else return num + Recur(num-1); } What is returned from the call Recur (-4)? Infinite Recursion: recursive calls that do not stop. Infinite Recursion! You will eventually run out of memory

17 Write recursive functions to print a string forwards and backwards.

18 Tail Recursion: a recursive algorithm in
// Recursive Method to print a string void Print (String word, int n) { } System.out.print( word.charAt(n)); if (n != word.length()-1) Print (word, n+1); Tail Recursion: a recursive algorithm in which no statements are executed after the return from the recursive call.

19 // Recursive Method to print a string backwards
void PrintBack (apstring word, int n) { } System.out.print( word.charAt(n)); if (n != 0) PrintBack (word, n-1);

20 Recursion : Wrap-up! takes more ___________________ and ___________________ reduces the ___________________ of a problem - recursive solutions are usually simpler solutions to a problem must have a ___________________ a recursive algorithm can be implemented nonrecursively by using _________________ nonrecursive solutions are often more efficient in terms of time and space, but are usually less ___________________. time space complexity base case iteration elegant

21 Class Exercise

22 return Combo (amount, value – 1) + Combo (amount – value, value);
int Combo (int amount, int value) { if ((amount < 0) || (value == 0)) return 0; else if (amount == 0) return 1; return Combo (amount, value – 1) + Combo (amount – value, value); } int main () cout << Combo (4, 3); What is output by the program? Work it out!

23 Hints! Power – you already know
PowerOfTwo – think num%2 and num/2 and test for remainder Twos - very similar to PowerOf Two except leave a count of 1 or 0 Binary – think n%2 and n/2


Download ppt "Recursion DRILL: Please take out your notes on Recursion"

Similar presentations


Ads by Google