Topic 9 Introduction to RecursionUnderneath the Hood.The Program StackMethods for IllustrationSlide 5Basic CPU OperationsMore on the Program StackActivation Records and the Program StackSlide 9Using RecursionA ProblemAttendance Question 2Sample Directory StructureCode for getDirectorySpace()Attendance Question 3Iterative getDirectorySpace()Simple Recursion ExamplesWisdom for Writing Recursive MethodsThe 3 plus 1 rules of RecursionWriting Recursive MethodsN!Factorial RecursivelyBig O and RecursionCommon Recurrence RelationsTracing Fact With the Program StackCalling fact with 4Calling fact with 3Calling fact with 2Calling fact with 1Calling fact with 0 and returning 1Returning 1 from fact(1)Returning 2 from fact(2)Returning 6 from fact(3)Returning 24 from fact(4)Calling System.out.printlnEvaluating Recursive MethodsSlide 37Slide 38Attendance Question 4Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Unplugged ActivityRecursion PracticeFinding the Maximum in an ArrayAttendance Question 5Your Meta Cognitive StateA Harder(??) ProblemMine SweeperThe update methodUpdate CodeCS 307 Fundamentals of Computer ScienceIntroduction to Recursion1Topic 9 Introduction to Recursion"To a man with a hammer, everything looks like a nail" -Mark TwainCS 307 Fundamentals of Computer ScienceIntroduction to Recursion2Underneath the Hood.CS 307 Fundamentals of Computer ScienceIntroduction to Recursion3The Program StackWhen you invoke a method in your code what happens when that method is completed?FooObject f = new FooObject();int x = 3;f.someFooMethod(x);f.someBarMethod(x);How does that happen? What makes it possible?CS 307 Fundamentals of Computer ScienceIntroduction to Recursion4Methods for Illustration200 public void someFooMethod(int z)201{ int x = 2 * z;202 System.out.println(x); }300 public void someBarMethod(int y) 301 { int x = 3 * y;302 someFooMethod(x);303 System.out.println(x); }CS 307 Fundamentals of Computer ScienceIntroduction to Recursion5The Program StackWhen your program is executed on a processor the commands are converted into another set of instructions and assigned memory locations.–normally a great deal of expansion takes place101 FooObject f = new FooObject();102 int x = 3;103 f.someFooMethod(x);104 f.someBarMethod(x);Von Neumann ArchitectureCS 307 Fundamentals of Computer ScienceIntroduction to Recursion6Basic CPU OperationsA CPU works via a fetch command / execute command loop and a program counterInstructions stored in memory (Just like data!)101 FooObject f = new FooObject();102 int x = 3;103 f.someFooMethod(x);104 f.someBarMethod(x);What if someFooMethod is stored at memory location 200?CS 307 Fundamentals of Computer ScienceIntroduction to Recursion7More on the Program Stack101 FooObject f = new FooObject();102 int x = 3;103 f.someFooMethod(x);104 f.someBarMethod(x);Line 103 is really saying go to line 200 with f as the implicit parameter and x as the explicit parameter When someFooMethod is done what happens?A.Program ends B. goes to line 103C. Goes back to whatever method called itCS 307 Fundamentals of Computer ScienceIntroduction to Recursion8Activation Records and the Program StackWhen a method is invoked all the relevant information about the current method (variables, values of variables, next line of code to be executed) is placed in an activation recordThe activation record is pushed onto the program stackA stack is a data structure with a single access point, the top.CS 307 Fundamentals of Computer ScienceIntroduction to Recursion9The Program StackData may either be added (pushed) or removed (popped) from a stack but it is always from the top.–A stack of dishes–which dish do we have easy access to?topCS 307 Fundamentals of Computer ScienceIntroduction to Recursion10Using RecursionCS 307 Fundamentals of Computer ScienceIntroduction to Recursion11A ProblemWrite a method that determines how much space is take up by the files in a directoryA directory can contain files and directoriesHow many directories does our code have to examine?How would you add up the space taken up by the files in a single directory –Hint: don't worry about any sub directories at firstDirectory and File classesin the Directory class:public File[] getFiles()public Directory[] getSubdirectories()in the File classpublic int getSize()Attendance Question 2How many levels of directories have to be visited?A.0B.UnknownC.InfiniteD.1E.8CS 307 Fundamentals of Computer ScienceIntroduction to Recursion12CS 307 Fundamentals of Computer ScienceIntroduction to Recursion13Sample Directory Structurescottmcs307m1.txtm2.txthwa1.htm a2.htm a3.htm a4.htmAPA.pdfAB.pdfCS 307 Fundamentals of Computer ScienceIntroduction to Recursion14Code for getDirectorySpace()public int getDirectorySpace(Directory d){ int total = 0;File[] fileList = d.getFiles();for(int i = 0; i < fileList.length; i++)total += fileList[i].getSize();Directory[] dirList = d.getSubdirectories();for(int i = 0; i < dirList.length; i++)total += getDirectorySpace(dirList[i]);return total;}Attendance Question 3Is it possible to write a non recursive method to do this?A.YesB.NoCS 307 Fundamentals of Computer ScienceIntroduction to Recursion15CS 307 Fundamentals of Computer ScienceIntroduction to Recursion16Iterative getDirectorySpace()public int getDirectorySpace(Directory d){ ArrayList dirs = new ArrayList();File[] fileList;Directory[] dirList;dirs.add(d);Directory temp;int total = 0;while( ! dirs.isEmpty() ){ temp = (Directory)dirs.remove(0);fileList = temp.getFiles();for(int i = 0; i < fileList.length; i++)total += fileList[i].getSize();dirList = temp.getSubdirectories();for(int i =0; i < dirList.length; i++)dirs.add( dirList[i] );}return total;}CS 307 Fundamentals of Computer ScienceIntroduction to Recursion17Simple Recursion ExamplesCS 307 Fundamentals of Computer ScienceIntroduction to Recursion18Wisdom for Writing Recursive MethodsCS 307 Fundamentals of Computer ScienceIntroduction to Recursion19The 3 plus 1 rules of Recursion1. Know when to stop2. Decide how to take one step3. Break the journey down into that step and a smaller journey4. Have faithFrom Common Lisp: A Gentle Introduction to Symbolic Computation by David TouretzkyCS 307 Fundamentals of Computer ScienceIntroduction to Recursion20Writing Recursive MethodsRules of Recursion1. Base Case: Always have at least one case that can be solved without using recursion2. Make Progress: Any recursive call
View Full Document