Download presentation
Presentation is loading. Please wait.
Published bySolomon Carpenter Modified over 9 years ago
1
Question of the Day What three letter word completes the first word and starts the second one: DON???CAR
2
Question of the Day What three letter word completes the first word and starts the second one: DON???CAR Key Key completes the first word (donkey) and a key starts a car
4
re-cur-sion re-cur-sion: Solving problems by breaking into identical, smaller problems and combining solutions after base case(s) reached. Recursion
5
Recursive stepbase case(s) Recursive step simplifies problem to base case(s) Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1)) After base case, combine solutions in recursive steps = 4 * (3 * 2) = 4 * 6 = 24 See Recursion Work
6
No different than usual coding had been doing At some point, recursive method calls itself Parameters & locals in frame & so specific to each call Once complete, execution returns to calling frame NOT Writing Recursive Code
7
Starting Recursive Solution Identify when solution easy with little/no work Checking for situation involves one or two operations Simple return statement with literal or no value base case(s) May find more than one of these base case(s) If you cannot determine, stop immediately That are many problems without recursive solution
8
Base Case(s) Example Base Case(s) Index leaves array with 0 or 1 entries If specific item searched for in array, stop at 0 entries base case 1 entry used as base case when each item will be used At last Node or null reference in Linked List base case(s) Just like with arrays, base case(s) are comparable int reaches 0 if working linearly through data base case If multiplying int each pass, base case at 1 0 or 1 characters left in a String
9
Recursive Step Starting Recursive Step base case(s) Determine state preceding each of base case(s) These cases should be almost as easy to solve base case(s) One step away from reaching base case(s) base case Stand-alone base case may not have prior state base case Identify single step advancing toward base case Coding much easier when same step used in all cases Know when each is used if different steps needed
10
Consider All Possible Inputs After defining recursive call solution will be using Must check that it works for all possible inputs For each input you examine, must consider Check that recursive call moves toward base case recursive steps No conflicting recursive steps that cycle within space
11
Consider All Possible Inputs After defining recursive call solution will be using Must check that it works for all possible inputs For each input you examine, must consider Check that recursive call moves toward base case recursive steps No conflicting recursive steps that cycle within space
12
Next to Last Step Examine how method combines recursive result No result for some recursive solutions, so can skip this Some other recursive solutions just return result Others require even more work to be performed When using recursion, fairly simple actions required If many or complex actions required, then… First ask yourself: is this solution actually recursive? Could it be more recursive & simpler is next question
13
Do not want to be locked into recursive solution Recursion often adds parameter to the signature Public method only has required parameters Linked list instance or array to be processed User is interested in testing some value Use private method to hold the extra variable Value or array index where method currently working Node in linked list to be processed by method Range or indices where method starts & stops Last Step
14
public static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minFollowing = findMin(a, j+1); return Math.min(a[j], minFollowing); } } Check This Out
15
public static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minFollowing = findMin(a, j+1); return Math.min(a[j], minFollowing); } } public static int findMin(int[] a) { } Check It Out
16
private static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minFollowing = findMin(a, j+1); return Math.min(a[j], minFollowing); } } public static int findMin(int[] a) { return findMin(a, 0); } Check It Out
17
Your Turn Get into your groups and complete activity
18
For Next Lecture Read GT4.1-4.2 for class on Friday Is all code really equal? How can we figure out which is faster? Week #7 weekly assignment available now Angel also has programming assignment #1 Pulls everything together and shows off your stuff NO!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.