Click here to load reader
Upload
beverly-stone
View
212
Download
0
Embed Size (px)
DESCRIPTION
Linked lists not always obvious to code Lots of references must be set (“aliased”) Easy to screw-up, especially by mixing ordering Very easy solution to help do this, however Begin by drawing picture of method does Write method skeleton (declaration & braces {} ) Start method by aliasing variable to each instance Now assign fields making them equal to your picture Double-check by comparing trace with your picture Using Linked Lists
Citation preview
LECTURE 21:RECURSION & LINKED LIST REVIEWCSC 212 – Data Structures
Easy to create & use Java support built-
in Immediate access
Can get any entry Size is fixed from
start
Recursive structure Need DNode & Dlist
Slower to access Usually need
traversal Size changes w/
data
Arrays Linked Lists
Linked Lists v. Arrays
Linked lists not always obvious to code Lots of references must be set (“aliased”) Easy to screw-up, especially by mixing
ordering Very easy solution to help do this,
however Begin by drawing picture of method does
Write method skeleton (declaration & braces {} ) Start method by aliasing variable to each
instance Now assign fields making them equal to
your picture Double-check by comparing trace with your
picture
Using Linked Lists
Identify two parts of all recursive solutions Cases when problem is trivial to solve
“base case(s)” Split into identical, smaller problems at
RECURSIVE step(s)When both parts cannot be determined,
stop Do not always have recursive solution for a
problem Make sure you will always reach base
case Infinite recursion occurs when no base case
reached…
Recursive Solutions
Infinite Recursion
Handles when only 1 possible solution remains 0 or 1 array entries remain to be processed Linked list node’s next null or trailer
(sentinel) At the last value to be processed by the
method
Writing Base Case
Performs simple operation with 1 or 2 data items Before recursive call(s) could perform the
operation… … or the recursive call(s) provide (some of)
the data Ultimately solves problem for data it
considers Mostly, uses recursive calls to do the work Something is wrong if always solving entire
problem Question becomes: WHAT DATA TO
CONSIDER? Needs to shrink with each recursive call Should be defined by method’s parameters
Writing Recursive Step(s)
Public method only has required parameters Linked list instance to be processed Reference to the array to be evaluated Important value whose result the user is
interested Private method may have more
parameters Value or array index where method starts
working Value or array index where method stop
working Range or indices where method starts &
stops
Which Case is Which?
public int findMin(int[] arr) { return findMin(arr, 0);
}
private int findMin(int[] arr, int loc) {if (loc == arr.length – 1) { return arr[loc]; } else { int minAfter = findMin(arr, loc + 1); if (minAfter < arr[loc]) { return minAfter; } else { return arr[loc]; }
}
Where Method Starts Work
public void printTri(int[] arr) { return printTri(arr, arr.length - 1);
}
private int printTri(int[] arr, int loc) {if (loc == 0) { System.out.println(arr[loc]);} else { for (int i = 0; i < loc; i++) { System.out.print(arr[i] + “ ”); } System.out.println(); printTri(arr, loc - 1);}
}
Where Method Stops Work
public boolean pal(String str) { return pal(str, 0, str.length() - 1);
}
private boolean pal(String str, int start, int end){if (start > end) { return true;} else { boolean plndrm = pal(str, start + 1, end - 1); char fst = str.charAt(start); char lst = str.charAt(end); return plndrm && (fst == lst);}
}
Range To Use
Start week #8 assignment Due by 5PM next Tuesday
Start programming assignment #3 Messages are not always sent to everyone!
Read sections 4.1-4.2 in book before class Is all code really equal?
How can we determine how fast code runs?
Before Next Lecture…
NO!NO!