DOC PREVIEW
UT CS 307 - Study Notes

This preview shows page 1-2-3-4-25-26-27-52-53-54-55 out of 55 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 StackWhen 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 StackWhen 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 OperationsA CPU works via a fetch command / execute command loop and a program counterInstructions 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 StackWhen 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 recordThe activation record is pushed onto the program stackA stack is a data structure with a single access point, the top.CS 307 Fundamentals of Computer ScienceIntroduction to Recursion9The Program StackData 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 ProblemWrite a method that determines how much space is take up by the files in a directoryA directory can contain files and directoriesHow 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 firstDirectory and File classesin the Directory class:public File[] getFiles()public Directory[] getSubdirectories()in the File classpublic int getSize()Attendance Question 2How 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 3Is 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 MethodsRules 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

UT CS 307 - Study Notes

Documents in this Course
Midterm 2

Midterm 2

15 pages

Midterm 1

Midterm 1

15 pages

Syllabus

Syllabus

24 pages

s

s

8 pages

Midterm 1

Midterm 1

14 pages

Midterm 2

Midterm 2

14 pages

Recursion

Recursion

14 pages

Midterm 1

Midterm 1

16 pages

Load more
Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?