21 Recursion

21 Recursion - Recursion Recursion used in Fractals 1 What...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Recursion: Recursion: used in Fractals 1 What will be displayed? What will be displayed? void setup() { randomNumber(); randomNumber(); } void draw(){}//not used void randomNumber() { println(random(1)); } 2 What will be displayed now? What will be displayed now? void setup() { randomNumber(); randomNumber(); } void draw(){}//not used void randomNumber() { println(random(1)); println(random(1)); randomNumber(); randomNumber(); } 3 The program shows a bunch of random The program shows a bunch of random numbers and then crashes the problem is it won't stop! void setup() { randomNumber(); randomNumber(); } void draw(){}//not used void randomNumber() { println(random(1)); println(random(1)); randomNumber(); randomNumber(); } 4 Recursion: A function that calls itself Recursion: A function that void randomNumber() { println(random(1)); println(random(1)); randomNumber(); randomNumber(); } Recursion is another way of making a loop Recursion is hard to control—it's very easy to create an infinite loop that never stops 5 In recursion, the "stopping point" is In recursion, the "stopping point" is called the base case void randomNumber(int times) { if(times == 0) if(times { println("Stop!"); println("Stop!"); } else else { println(random(1)); println(random(1)); randomNumber(times-1); randomNumber(times-1); } }The base case stops the recursive calls 6 void setup() void { randomNumber(5); randomNumber(5); } void randomNumber(int times) { if(times == 0) if(times { println("Stop!"); println("Stop!"); } else else { println(random(1)); println(random(1)); randomNumber(times-1); randomNumber(times-1); } } 7 Recursion Recursion There are two basic ways to make things happen "over and over again" in programming Loops Recursion In theory, anything you can do with loops, you can do with recursion (& vice versa) 8 A loop that "counts" from 1 to 10 A loop that "counts" from 1 to 10 void setup() { for(int i = 1; i <= 10; i++) for(int { println(i); println(i); } } 9 A Recursive Function that A Recursive Function that "counts" from 1 to 10 void setup() { recursiveFunction(1); recursiveFunction(1); } void recursiveFunction(int num) { if(num == 10) if(num { println(num + " and stop"); println(num } else else { println(num); println(num); recursiveFunction(num+1); recursiveFunction(num+1); } } 10 A Recursive Function calls itself A Recursive Function void recursiveFunction(int num) void recursiveFunction { if(num == 10) if(num { println(num + " and stop"); println(num } else else { println(num); println(num); recursiveFunction(num+1); recursiveFunction } } 11 Just like a loop, it has a starting Just like a loop, it has a point, a stopping point, and a way to get from one to the other void setup() { recursiveFunction(1); recursiveFunction( } void recursiveFunction(int num) { if(num == 10) if(num { println(num + " and stop"); println(num } else else { println(num); println(num); recursiveFunction(num+1); recursiveFunction( } } 12 base case base case Just like a loop, if recursion doesn't stop, we'll crash the computer void recursiveFunction(int num) { if(num == 10) if(num { println(num + " and stop"); println(num } else else { println(num); println(num); recursiveFunction(num+1); recursiveFunction(num+1); } } The if statement is called the base case In the base case there is no recursive call, and the recursion stops 13 What is the output? What is the output? void setup() { println(mystery(4)); println(mystery(4)); } int mystery(int num) { if(num <= 1) if(num { return 1; return } else else { return num * mystery(num - 1); return } } 14 To understand, start with the base To understand, start with the case What would mystery(1) return? return? int mystery(int num) { if(num <= 1) if(num { return 1; return } else else { return num * mystery(num - 1); return } } 15 To understand, start with the base To understand, start with the case 1 What would mystery(1) return? return? int mystery(int num) { if(num <= 1) if(num { return 1; return } else else { return num * mystery(num - 1); return } } 16 To understand, start with the base To understand, start with the case 1 What would mystery(1) return? return? What would mystery(2) return? return? int mystery(int num) { if(num <= 1) if(num { return 1; return } else else { return num * mystery(num - 1); return } } 17 To understand, start with the base To understand, start with the case 1 What would mystery(1) return? return? What would mystery(2) return? return? 2 * mystery(1) = 2 * 1 = 2 int mystery(int num) { if(num <= 1) if(num { return 1; return } else else { return num * mystery(num - 1); return } } 18 To understand, start with the base To understand, start with the case What would mystery(1) return? 1 return? What would mystery(2) return? return? 2 * mystery(1) = 2 * 1 = 2 What would mystery(3) return? return? int mystery(int num) { if(num <= 1) if(num { return 1; return } else else { return num * mystery(num - 1); return } } 19 To understand, start with the base To understand, start with the case What would mystery(1) return? 1 return? What would mystery(2) return? return? 2 * mystery(1) = 2 * 1 = 2 What would mystery(3) return? return? 3 * mystery(2) = 3 * 2 = 6 int mystery(int num) { if(num <= 1) if(num { return 1; return } else else { return num * mystery(num - 1); return } } 20 To understand, start with the base To understand, start with the case What would mystery(4) return? return? int mystery(int num) { if(num <= 1) if(num { return 1; return } else else { return num * mystery(num - 1); return } } 21 To understand, start with the base To understand, start with the case What would mystery(4) return? return? 4 * mystery(3) = 4 * 6 = 24 int mystery(int num) { if(num <= 1) if(num { return 1; return } else else { return num * mystery(num - 1); return } } 22 Practice Quiz Question: Practice Quiz Question: Find the output void setup() { println(mystery(3)); println(mystery(3)); } int mystery(int num) { if(num <= 1) if(num { return 1; return } else else { return num + mystery(num - 1); return } } 23 Recursive Functions Recursive Functions A Recursive Function is a Function that calls itself: int mystery(int nNum) int mystery { if(nNum == 0) if(nNum { return 0; return } else else { return 2 + mystery(nNum - 1); mystery } 24 } Recursive Functions Recursive Functions A Recursive Function is a Function that calls itself: int mystery(int nNum) int mystery { if(nNum == 0) if(nNum { return 0; return } else else { return 2 + mystery(nNum - 1); mystery } 25 } Recursive Functions Recursive Functions What would be returned by the call mystery(0)? int mystery(int nNum) int mystery { if(nNum == 0) if(nNum { return 0; return } else else { return 2 + mystery(nNum - 1); mystery } 26 } Recursive Functions Recursive Functions What would be returned by the call mystery(1)? int mystery(int nNum) int mystery { if(nNum == 0) if(nNum { return 0; return } else else { return 2 + mystery(nNum - 1); mystery } 27 Recursive Functions Recursive Functions mystery(1) = 2 + mystery(0) = 2+0 int mystery(int nNum) int mystery { if(nNum == 0) if(nNum { return 0; return } else else { return 2 + mystery(nNum - 1); mystery } } 28 Recursive Functions Recursive Functions What would be returned by the call mystery(2)? int mystery(int nNum) { if(nNum == 0) if(nNum { return 0; return } else else { return 2 + mystery(nNum - 1); return 29 Recursive Functions Recursive Functions mystery(2) = 2 + mystery(1) = 2 + 2 + mystery(0)= 2 + 2 + 0 int mystery(int nNum) int mystery { if(nNum == 0) if(nNum { return 0; return } else else { return 2 + mystery(nNum - 1); mystery } } 30 Recursive Functions Recursive Functions What would be returned by the call mystery(10)? 31 Recursive Functions Recursive Functions = = = = = = = = What would be returned by the call mystery(10)? mystery(10) = 2 + mystery(9) 2+2+mystery(8) 2+2+2+mystery(7) 2+2+2+2+mystery(6) 2+2+2+2+2+mystery(5) 2+2+2+2+2+2+mystery(4) 2+2+2+2+2+2+2+mystery(3) 2+2+2+2+2+2+2+2+mystery(2) 2+2+2+2+2+2+2+2+2+mystery(1) 32 = 2+2+2+2+2+2+2+2+2+2+mystery(0)=20 Recursive Functions Recursive Functions Let's remove some code from mystery Now what would be returned by the call mystery(10)? int mystery(int int mystery { if(nNum == 0) { return 0; return } else { return 2 + } } nNum) //delete 3 lines mystery(nNum - 1); mystery 33 Recursive Functions Recursive Functions mystery(10) = 2 + mystery(9) = 2+2+mystery(8) = 2+2+2+mystery(7) = 2+2+2+2+mystery(6) = 2+2+2+2+2+mystery(5) = 2+2+2+2+2+2+mystery(4) = 2+2+2+2+2+2+2+mystery(3) = 2+2+2+2+2+2+2+2+mystery(2) = 2+2+2+2+2+2+2+2+2+mystery(1) = 2+2+2+2+2+2+2+2+2+2+mystery(0) = 2+2+2+2+2+2+2+2+2+2+2+mystery(-1) = 2+2+2+2+2+2+2+2+2+2+2+2+mystery(-2) … and so on, and so on Infinite Recursion! Stack Overflow! 34 CRASH! The Base Case The Base Case Every recursive Function must have a base case The base case is where the recursion stops int mystery(int nNum) { if(nNum == 0) //base case if(nNum { return 0; return } else else { return 2 + mystery(nNum - 1); return } } 35 Problem: write a program to display Problem: write a program to display the first 20 fibonacci numbers 0,1,1,2,3,5,8,13,21,. . . 36 Problem: write a program to display Problem: write a program to display the first 20 Fibonacci numbers 0,1,1,2,3,5,8,13,21,. . . after 0 and 1, each successive Fibonacci number is the sum of the two previous Fibonacci numbers 37 Problem: write a program to display Problem: write a program to display the first 20 Fibonacci numbers 0,1,1,2,3,5,8,13,21,. . . after 0 and 1, each successive Fibonacci number is the sum of the two previous Fibonacci numbers Position 0 1 2 3 4 5 6 7 8 Fibonacci 0 1 1 2 3 5 8 13 21 38 Problem: write a program to display Problem: write a program to display the first 20 Fibonacci numbers void setup() { for(int i = 0; i < 20;i++) for(int { print(fibonacci(i)+ ", "); print(fibonacci(i)+ } } int fibonacci(int num) { ?? ?? } 39 Problem: write a program to display Problem: write a program to display the first 20 Fibonacci numbers What's the base case? int fibonacci(int num) { ?? ?? } 40 Problem: write a program to display Problem: write a program to display the first 20 Fibonacci numbers Position 1 2 3 4 5 6 7 8 Fibonacci 0 0 1 1 2 3 5 8 13 21 What's the base case? int fibonacci(int num) { if(num == 0) if(num { return } ?? ?? 41 Position 1 2 3 4 5 6 7 8 Fibonacci 0 0 1 1 2 3 5 8 13 21 What about when num is 1? int fibonacci(int num) { if(num == 0) if(num { return 0; return } else if(num == 1) else { return ?? ?? 42 Problem: write a program to display the first 20 Fibonacci numbers Problem: write a program to display the first 20 Fibonacci numbers Position 4 5 6 7 8 Fibonacci 01123 From now on, add the two previous numbers int fibonacci(int num) { if(num == 0) if(num { return 0; return } else if(num == 1) else { return 1; return } else else { 5 8 13 21 } } return 0 1 2 3 ?? ?? 43 Problem: write a program to display the first 20 Fibonacci numbers Problem: write a program to display the first 20 Fibonacci numbers Position 4 5 6 7 8 Fibonacci 01123 From now on, add the two previous numbers int fibonacci(int num) { if(num == 0) if(num { return 0; return } else if(num == 1) else { return 1; return } else else { 5 8 13 21 } } 0 return fibonacci( 1 2 3 ?? ); ?? 44 Problem: write a program to display the first 20 Fibonacci numbers Problem: write a program to display the first 20 Fibonacci numbers Position 0 1 2 3 4 5 6 7 8 Fibonacci 0 1 1 2 3 5 8 13 21 • From now on, add the two previous numbers int fibonacci(int num) int { if(num == 0) if(num { return 0; return } else if(num == 1) else { return 1; return } else else { return fibonacci(num - 1) + fibonacci(num - 2); fibonacci(num } } 45 Problem: write a program to display the first 20 Fibonacci numbers Problem: write a program to display the first 20 Fibonacci numbers void setup() { for(int i = 0; i < 20;i++) for(int { print(fibonacci(i)+ ", "); print(fibonacci(i)+ } } int fibonacci(int num) { if(num == 0) if(num { return 0; return } else if(num == 1) else { return 1; return } else else { return fibonacci(num - 1) + fibonacci(num - 2); return } } 46 Find the output Find the output (Hint: start with the base case) void setup() { println(mystery(0)); println(mystery(0)); } int mystery(int num) { if(num == 5) if(num { return 5; return } else else { return 2 + mystery(num+1); return } } 47 ...
View Full Document

This note was uploaded on 01/13/2012 for the course CS 12 taught by Professor Ma during the Spring '08 term at UC Davis.

Ask a homework question - tutors are online