IAT 265 Recursion June 18, 2015 IAT 265 1 Today’s Excitement Recursion June 18, 2015 IAT 265 2 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 ); } June 18, 2015 IAT 265 3 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 ); } June 18, 2015 IAT 265 4 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int factorial(int n) (n=2) { { if(n > 1) if(n > 1) { { return( n* factorial( n-1 ) ); return( n* factorial( n-1 ) ); } } else else return( 1 ); return( 1 ); } } June 18, 2015 IAT 265 5 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int factorial(int n) (n=2) int factorial(int n) (n=1) { { { if(n > 1) if(n > 1) if(n > 1) { { { return( n* factorial( n-1 ) ); return( n* factorial( n-1 ) ); return( n* factorial( n-1 } } } else else else return( 1 ); return( 1 ); return( 1 ); } } } June 18, 2015 IAT 265 6 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int factorial(int n) (n=2) int factorial(int n) (n=1) { { { if(n > 1) if(n > 1) if(n > 1) { { { return( n* factorial( n-1 ) ); return( n* factorial( n-1 ) ); return( n* factorial( n-1 } } } else else else return( 1 ); return( 1 ); return( 1 ); } } } June 18, 2015 IAT 265 7 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int factorial(int n) (n=2) { { if(n > 1) if(n > 1) { { return( n* factorial( n-1 ) ); return( n* factorial( n-1 ) ); } } else else return( 1 ); return( 1 ); } } June 18, 2015 IAT 265 1 8 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int factorial(int n) (n=2) { { if(n > 1) if(n > 1) { { return( n* factorial( n-1 ) ); return( n* 1 ); } } else else return( 1 ); return( 1 ); } } June 18, 2015 IAT 265 9 Calling Itself Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int factorial(int n) (n=2) { { if(n > 1) if(n > 1) { { return( n* factorial( n-1 ) ); return( 2* 1 ); } } else else return( 1 ); return( 1 ); } } June 18, 2015 IAT 265 10 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 ); } June 18, 2015 2 IAT 265 11 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 ); } June 18, 2015 IAT 265 12 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 ); } June 18, 2015 IAT 265 13 Calling Itself Let’s step through what happens. factorial(3); 6 June 18, 2015 IAT 265 14 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 June 18, 2015 IAT 265 15 Web Crawling HTML reader called parsePage() – Reads HTML – Finds links – Per Link it should • Call parsePage() June 18, 2015 IAT 265 16 Web Crawling 1 2 4 3 June 18, 2015 IAT 265 5 17 Web Crawling 1 6 2 5 4 3 7 11 13 10 8 June 18, 2015 14 9 15 12 IAT 265 18 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 June 18, 2015 IAT 265 19 Recursion Remember—base cases prevent infinite cats. http://infinitecat.com/ June 18, 2015 IAT 265 20