Download presentation
Presentation is loading. Please wait.
Published byKory Charles Modified over 9 years ago
1
LECTURE 20: RECURSION CSC 212 – Data Structures
2
Humorous Asides
3
“A journey begins with a single step”
5
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 if s and loops Action in many methods into small (private) methods Solving Problems
6
(At least for programming) Smaller is Better CENSORED
7
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
8
re-cur-sion: Method of solving problem by combining solutions to identical, smaller problems Recursion
9
Keep dividing until base case(s) reached Base cases are cases that are trivial to solve 4! = 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
10
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
11
Will your solution work? Each step moves toward base case All possible inputs must be considered Otherwise, get infinite recursion Working Recursive Solutions
12
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
13
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
14
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
15
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 N O CURING CANCER Tracing Recursion X
16
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!
17
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!
18
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
19
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…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.