Download presentation
Presentation is loading. Please wait.
Published byCynthia O’Brien’ Modified over 9 years ago
1
June 18, 2015IAT 2651 Recursion
2
June 18, 2015IAT 2652 Today’s Excitement Recursion
3
June 18, 2015IAT 2653 Recursion Recursion basically means calling a method from inside itself. int factorial(int n) { if( n > 1 ) { return( n* factorial( n-1 ) ); } else return( 1 ); }
4
June 18, 2015IAT 2654 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); }
5
June 18, 2015IAT 2655 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); } int factorial(int n) (n=2) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); }
6
June 18, 2015IAT 2656 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); } int factorial(int n) (n=2) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); } int factorial(int n) (n=1) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); }
7
June 18, 2015IAT 2657 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); } int factorial(int n) (n=2) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); } int factorial(int n) (n=1) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); }
8
June 18, 2015IAT 2658 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); } int factorial(int n) (n=2) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); } 1
9
June 18, 2015IAT 2659 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); } int factorial(int n) (n=2) { if(n > 1) { return( n* 1 ); } else return( 1 ); }
10
June 18, 2015IAT 26510 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); } int factorial(int n) (n=2) { if(n > 1) { return( 2* 1 ); } else return( 1 ); }
11
June 18, 2015IAT 26511 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); } 2
12
June 18, 2015IAT 26512 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( n* 2 ); } else return( 1 ); }
13
June 18, 2015IAT 26513 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( 3* 2 ); } else return( 1 ); }
14
June 18, 2015IAT 26514 Calling Itself Let’s step through what happens. factorial(3); 6
15
June 18, 2015IAT 26515 Base Case Must have Base Case –A case or condition that returns without further recursion –Stops the recursive chain –Eg factorial( int n ) Returned 1 when n = 1 In every other call, n decreases by 1
16
June 18, 2015IAT 26516 Web Crawling HTML reader called parsePage() –Reads HTML –Finds links –Per Link it should Call parsePage()
17
June 18, 2015IAT 26517 Web Crawling 1 3 4 5 2
18
June 18, 2015IAT 26518 Web Crawling 1 3 4 5 2 6 8 7 9 10 11 12 13 14 15
19
June 18, 2015IAT 26519 Web Crawling What base case? –Count the number of recursive calls so far –Place a limit on depth –Explore no further after depth 4 –Example stopped at depth 2
20
June 18, 2015IAT 26520 Recursion Remember—base cases prevent infinite cats. http://infinitecat.com/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.