lecture 20: recursion csc 212 – data structures. humorous asides
DESCRIPTION
“A journey begins with a single step”TRANSCRIPT
LECTURE 20:RECURSIONCSC 212 – Data Structures
Humorous Asides
“A journey begins with a single step”
“A journey begins with a single step”
Large problems hard to solve Thinking about & solving small problems
easier Split large problems into smaller ones
Before you start coding, plan each assignment
Break up large methods with many ifs and loops
Action in many methods into small (private) methods
Solving Problems
(At least for programming)
Smaller is Better
CENSORED
Should be boring, monotonous drone Given its parameters, perform the expected
action Only perform the action defined for its
parameters Should not cure cancer
Do not worry about the larger problem This greater solution is not this method’s concern
Solving entire programming assignment is hard Why we divide up tasks in the first place!
Goal of a Java Method
re-cur-sion: Method of solving problem by combining solutions to identical, smaller problems
Recursion
Keep dividing until base case(s) reached Base cases are cases that are trivial to solve4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!))
After base case, combine solutions to complete work = 4 * (3 * (2 * 1)) = 4 * (3 * 2) = 4 * 6 = 24
See Recursion Work
Begin by figuring out what are the base case(s) Simple subproblem that is solved
immediately A base case will always be reached solving
problem Recursive step can then be generated
Create easier problem(s) which splits original problem
Combine subproblems’ solutions for final answer
Writing Recursive Solution
Will your solution work? Each step moves toward base case All possible inputs must be considered Otherwise, get infinite recursion
Working Recursive Solutions
A method is recursive if it calls itself:public static int factorial(int i) { if (i <= 1) {
return 1; } else {
return i * factorial(i - 1); }
}
Recursion in Java
public method starts the recursive process Usually only calls recursive method
private recursive method has 1+ parameter Results should only depend on the parameters if or switch selects from different cases
Using fields can avoid parameters… … but not recursive approach to solving the
problem Cheating recursion with fields does not scale
Designing Recursion
Should have immediately check for base case(s) Cannot have recursive calls within this code
Include 1+ method calls in recursive step(s) Assume recursion works when writing these Take 1 step toward base case (not 2, 3, or
10482)
Writing Recursive Method
Each call gets frame created for it Within frame, place all local variables &
parameters (Real frames include line being executed)
Trace shows frames solving their single step NO CURING CANCER
Tracing Recursion
X
private static void rev(int[] a, int j) {int opposite = a.length - (j + 1);if (j < opposite) { rev(a, j + 1); int temp = a[j]; a[j] = a[opposite]; a[opposite] = temp;}
}public static void reverse(int[] a) { rev(a,0);
}
int[] example1 = { 0 };reverse(example1);
Trace This, Buddy!
private static void rev(int[] a, int j) {int opposite = a.length - (j + 1);if (j < opposite) { rev(a, j + 1); int temp = a[j]; a[j] = a[opposite]; a[opposite] = temp;}
}public static void reverse(int[] a) { rev(a,0);
}
int[] example2 = { 0, 1, 2, 3, 4 };reverse(example2);
Trace This, Buddy!
Divide array in two parts First element & everything after
Last element & all preceding elements
Left & right halves of the array
Base case when working with 0 (or 1) entries
Recursive Array Handling
Complete week #7 assignment Due by 5PM next Wednesday
Finish programming assignment #2 Due before next lecture (Wednesday)
Review sections 3.1-3.5 in book before class How do linked lists, arrays & recursion
work together? How do they work?
Before Next Lecture…