Recursion English Return Oxford Webster procedure repeating itself indefinitely or until condition met such as grammar rule Webster adequate satisfactory satisfactory adequate recursion recursion Mathematics expression giving successive terms of a series Oxford Programming Method calling itself On a smaller problem Alternative to loops Recursion Recursive Functions Recursive Procedures Number based Recursion List based Recursion factoroial n 1 2 3 4 n public static int factorial int n int product 1 while n 0 product n n 1 return product public static void main String args while true loop condition never false int n Keyboard readInt if n 0 break System out println factorial factorial n Defining factorial n Product of the first n numbers 1 2 3 4 n factorial 0 1 factorial 1 1 1 factorial 0 factorial 2 2 1 2 factorial 1 factorial 3 3 2 1 3 factorial 2 factorial 4 4 3 2 1 4 factorial 3 factorial n n n 1 1 n factorial n 1 Defining factorial n factorial n 1 factorial n n factorial n 1 if n 0 if n 0 Implementing factorial n edit factorial n 1 factorial n n factorial n 1 if n 0 if n 0 public static int factorial int n Implementing factorial n edited factorial n 1 factorial n n factorial n 1 if n 0 if n 0 public static int factorial int n if n 0 return 1 if n 0 return n factorial n 1 Implementing factorial n factorial n factorial n n factorial n 1 n 0 Function must return something for all cases 1 if n 0 if n 0 public static int factorial int n if n 0 return 1 if n 0 return n factorial n 1 Implementing factorial n factorial n factorial n n factorial n 1 if n 0 factorial n factorial n if n 0 Base case Recursive reduction steps 1 if n 0 public static int factorial int n if n 0 return 1 else if n 0 return factorial n else return n factorial n 1 General form of Recursive Method if base case 1 return solution for base case 1 else if base case 2 return solution for base case 2 else if base case n return solution for base case n else if recursive case 1 do some preprocessing recurse on reduced problem do some postprocessing else if recursive case m do some preprocessing recurse on reduced problem do some postprocessing Recursion Vs Loops Iteration public static int factorial int n int product 1 while n 0 product n n 1 return product Implementation follows from definition public static int factorial int n if n 0 return 1 else if n 0 return factorial n else return n factorial n 1 Tracing Recursive Calls Stack Tracing Recursive Calls Invocation Invocation Invocation Invocation Invocation factorial 0 factorial 0 factorial 0 factorial 0 factorial 1 factorial 1 factorial 1 factorial 1 factorial 1 factorial 2 factorial 2 factorial 2 factorial 2 factorial 2 nnnnn 01000 12111 2222 return value return value return value return value return value 1 11 11 2 Recursion Pitfalls public static int factorial int n return n factorial n 1 factorial 2 2 factorial 1 1 factorial 0 0 factorial 1 Infinite recursion Stack overflow No base case 1 factorial 2 Recursion Pitfalls public static int factorial int n if n 0 return 1 else if n 0 return factorial n else return factorial n 1 n 1 factorial 2 factorial 3 3 factorial 4 4 factorial 5 5 Infinite recursion Recurses on bigger problem factorial 6 6 Recursive Methods Should have base case s Recurse on smaller problem s recursive calls should converge to base case s Recursive Functions with Multiple Parameters power base exponent baseexponent base base base base exponent of times power 0 exponent power 1 exponent power 2 exponent power 3 exponent 0 1 2 2 2 2 exponent times 3 3 3 3 exponent times No pattern Recursive Functions with Multiple Parameters edit power base exponent baseexponent base base base base exponent of times Recursive Functions with Multiple Parameters edited power base exponent baseexponent base base base base exponent of times power base 0 1 power base 1 base 1 base power base 0 power base 2 base base 1 base power base 1 power base exponent base power base exponent 1 Recursive Functions with Multiple Parameters power base exponent baseexponent base base base base exponent of times power base 0 1 power base 1 base 1 base power base 0 power base 2 base base 1 base power base 1 power base exponent base power base exponent 1 Defining power base exponent power base exponent power base exponent 1 base power base exponent 1 public static int power int base exponent if n 0 return base else return base power base exponent 1 if exponent 0 if exponent 0 Recursive Procedures greet n greet 0 greet 1 greet 2 print hello print hello print hello greet n print hello n times print hello print hello print hello Defining greet n edit greet 0 greet 1 greet 2 print hello print hello print hello greet n print hello n times print hello print hello print hello Defining greet n edited greet 0 greet 1 greet 2 print hello print hello greet n print hello print hello n times Do nothing print hello print hello Print hello greet 0 print hello Print hello greet 1 greet n 1 Print hello Print hello greet n 1 Defining greet n greet 0 greet 1 greet 2 print hello print hello print hello do nothing greet n print hello n times print hello print hello print hello greet 0 print hello greet 1 print hello greet n 1 print hello Defining greet n greet n greet n do nothing greet n 1 print hello if n 0 if n 0 Implementing greet n edit greet n do nothing greet n greet n 1 print hello if n 0 if n 0 Implementing greet n edited greet n greet n do nothing greet n 1 print hello if n 0 if n 0 Public static void greet int n if n 0 System out println hello greet n 1 Implementing greet n greet n greet n do nothing greet n 1 print hello public static void greet int n if n 0 greet n 1 System out println hello if n 0 if exponent 0 List based recursion multiplyList multiplyList 1 if remaining input is 1 multiplyList 2 if remaining input is 2 1 multiplyList 2 6 if remaining input is 2 6 1 List based recursion multiplyList edited multiplyList 1 if nextVal 0 multiplyList 2 if input is 2 1 multiplyList 2 6 multiplylist readNextVal multiplylist if input is 2 6 1 if nextVal 0 List based recursion multiplyList multiplyList multiplyList 1 readNextVal multiplyList public static int multiplyList int nextVal Keyboard readInt if nextVal 0 return 1 else return nextVal multiplyList if nextVal 0 if nextVal 0 Tracing multiplyList public static int multiplyList int nextVal Keyboard readInt if nextVal 0 return 1 else return nextVal multiplyList Invocation Invocation Invocation multiplyList multiplyList multiplyList multiplyList
View Full Document