1 / 20

IAT 800

IAT 800. Recursion. Today’s Excitement. Recursion. 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 ); }. Calling Itself.

barry-colon
Download Presentation

IAT 800

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IAT 800 Recursion IAT 800

  2. Today’s Excitement • Recursion IAT 800

  3. 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 ); } IAT 800

  4. 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 ); } IAT 800

  5. 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 ); } IAT 800

  6. 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 ); } IAT 800

  7. 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 ); } IAT 800

  8. 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 IAT 800

  9. 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 ); } IAT 800

  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 ); } int factorial(int n) (n=2) { if(n > 1) { return( 2* 1 ); } else return( 1 ); } IAT 800

  11. 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 IAT 800

  12. 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 ); } IAT 800

  13. 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 ); } IAT 800

  14. Calling Itself • Let’s step through what happens. factorial(3); 6 IAT 800

  15. 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 IAT 800

  16. Web Crawling • HTML reader called parsePage() • Reads HTML • Finds links • Per Link it should • Call parsePage() IAT 800

  17. Web Crawling 1 2 5 4 3 IAT 800

  18. Web Crawling 1 6 2 5 4 3 7 11 13 15 10 14 9 8 12 IAT 800

  19. 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 IAT 800

  20. Recursion Remember—base cases prevent infinite cats.http://infinitecat.com/ IAT 800

More Related