- 375 Views
- Uploaded on
- Presentation posted in: General

Recursion

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Recursion

Brian Toone

10/14/09

- When a method calls itself
- Example
int factorial(n) {

if (n<=1)

return 1;

else

return n*factorial(n-1);

}

- All recursion must end
- Base case
- The condition which terminates the recursion
- Each recursive call should make progress towards base case
- Factorial – the base case occurs when n <= 1
int factorial(n) {

if (n<=1)

return 1;

else

return n*factorial(n-1);

}

- Display all possible orderings of the letters in the word “carbon”
- Recursive solution
- Concatenate ‘c’ to the beginning of each ordering returned from a recursive call made on “arbon”
- Concatenate ‘a’ to the beginning of each ordering returned from a recursive call made on “crbon”
- The recursive call is made to obtain the orderings of the subwords “arbon”, “crbon”, “cabon”, “caron”
- Base case: only one letter - simply return it