slides created by Alyssa Harding

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Advertisements

Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CSE 143 Lecture 11 Recursive Programming reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 12 Lecture 12-2: recursive programming reading:
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
CSE 143 Lecture 13 Recursive Programming reading: slides created by Marty Stepp
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Contest Algorithms January 2016 Pseudo-code for divide and conquer, and three examples (binary exponentiation, binary search, and mergesort). 5. Divide.
CSE 143 Lecture 14 Maps/Sets; Grammars reading: slides adapted from Marty Stepp and Hélène Martin
CS1101: Programming Methodology Preparing for Practical Exam (PE)
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Chapter 18 Recursion CS1: Java Programming Colorado State University
Sections 4.1 & 4.2 Recursive Definitions,
Recursion Version 1.0.
Lecture 19: Binary Trees reading: 17.1 – 17.3
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Introduction to programming in java
Lecture 10: recursive programming reading:
MSc/ICY Software Workshop , Semester 2
Exponents Scientific Notation
Programming in Java: lecture 10
CS1101X: Programming Methodology Recitation 8 Recursion I
Java Programming: Program Design Including Data Structures
Building Java Programs
slides created by Marty Stepp and Alyssa Harding
Design by Contract Fall 2016 Version.
Building Java Programs
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CS 177 Week 15 Recitation Slides
Building Java Programs
Writing Methods AP Computer Science A.
Lecture 10: recursive programming reading:
CSE 143 Lecture 14 (A) More Recursive Programming
CSE 143 Lecture 11 Recursive Programming reading:
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
slides created by Alyssa Harding
Building Java Programs
CSE 373 Data Structures and Algorithms
CSE 143 Lecture 14 More Recursive Programming; Grammars
CSE 143 Lecture 11 Recursive Programming slides created by Marty Stepp
Java Lesson 36 Mr. Kalmes.
slides created by Alyssa Harding
slides created by Marty Stepp and Alyssa Harding
Building Java Programs
Recursion based on slides by Alyssa Harding
slides created by Ethan Apter
Recursion Problems.
slides created by Alyssa Harding
Recursion Based on slides by Alyssa Harding
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Recursion (Continued)
slides created by Alyssa Harding
Programming with inheritance Based on slides by Alyssa Harding
CSE 143 Lecture 13 Recursive Programming reading:
CSE 143 Lecture 13 Recursive Programming reading:
Building Java Programs
CSE 143 Lecture 2 ArrayIntList, binary search
Generics, Stack, Queue Based on slides by Alyssa Harding
slides created by Ethan Apter
Lecture 10: recursive programming reading:
Announcement The fourth test will be 75 minutes, will consist of two parts and will take place next week. The programming part will be about Chapter 2-6,
Lecture 22: Number Systems
slides created by Ethan Apter and Marty Stepp
Recursion Chapter 12.
Presentation transcript:

slides created by Alyssa Harding CSE 143 Lecture 10 Recursion slides created by Alyssa Harding http://www.cs.washington.edu/143/

Example: writeBinary Before we look at binary, a visual review of decimal numbers: We get 348 by adding powers of 10 348 = 300 + 40 + 8 = 3 ×102 + 4 ×101 + 8×100 That’s why the decimal system is base 10

Example: writeBinary Binary is exactly the same, but base 2 Decimal Sum 0 × 20 1 1 × 20 2 10 1 × 21 + 0 × 20 3 11 1 × 21 + 1 × 20 4 100 1 × 22 + 0 × 21 + 0 × 20 5 101 1 × 22 + 0 × 21 + 1 × 20 6 110 1 × 22 + 1 × 21 + 0 × 20 7 111 1 × 22 + 1 × 21 + 1 × 20 8 1000 1 × 23 + 0 × 22 + 0 × 21 + 0 × 20

Example: writeBinary So, what is 8574 in binary? ….anyone? How about the last digit? It’ll be 0, just like all the other even numbers in the table

Example: writeBinary This gives us an idea of how to break our problem down Recall that we can split a decimal number to get the leading digits in base-10 and the final digit: To get the leading digits in base-2 and the final digit, we can do the same: 857 4 n/10 n%10 4287 n/2 n%2

Example: writeBinary So, what’s the base case? public static void writeBinary(int n) { if ( n < 2 ) { System.out.print(n); } else { ... } } We know what to do with a single binary digit, 0 or 1

Example: writeBinary What’s the recursive case? public static void writeBinary(int n) { if ( n < 2 ) { System.out.print(n); } else { ... System.out.print(n%2); } } If we have a bigger number, we can still print out its last binary digit

Example: writeBinary What’s the recursive case? public static void writeBinary(int n) { if ( n < 2 ) { System.out.print(n); } else { writeBinary(n/2); System.out.print(n%2); } } And we can recurse to print out the remaining part of the number

Example: writeBinary What’s the recursive case? public static void writeBinary(int n) { if ( n < 2 ) { System.out.print(n); } else { writeBinary(n/2); System.out.print(n%2); } } And we can recurse to print out the remaining part of the number

Example: pow Now we want to recursively take x to the y power We’ll only deal with ints, so x and y must be ints This also means that y must be positive so that the return value is an int

Example: pow What’s the base case? // returns x to the y power // pre: y >= 0 public static int pow(int x, int y) { if ( y == 0 ) { return 1; } else { ... } } If y is 0, we automatically know that the power is 1!

Example: pow What’s the recursive case? // returns x to the y power // pre: y >= 0 public static int pow(int x, int y) { if ( y == 0 ) { return 1; } else { return x * pow(x,y-1); } } Otherwise, we can break it down into two smaller problems: xy = x1 × xy-1

Example: pow Taking care of negative inputs... // returns x to the y power // if y < 0, throws IllegalArgumentException public static int pow(int x, int y) { if ( y < 0 ) { throw new IllegalArgumentException(); } else if ( y == 0 ) { return 1; } else { return x * pow(x,y-1); } }

Example: pow But is this the best way? If I asked you to compute 232, you wouldn’t hit multiply on your calculator 32 times (hopefully!) We can also note that xy = (x2)(y/2) Example: 34 = 92 = 81 This only works for even values of y because of integer division

Example: pow Putting in our new idea... public static int pow2(int x, int y) { if ( y < 0 ) { throw new IllegalArgumentException(); } else if ( y == 0 ) { return 1; } else if ( y % 2 == 0 ) { return pow(x*x, y/2); } else { return x * pow2(x,y-1); } }

Example: pow Why is this better? Consider calculating 2 to the billionth power (109) Our original algorithm would be like hitting “* 2” on your calculator a billion times This is an O(n) algorithm The second algorithm decreases y by half each time, a much bigger gain. This is like binary search, when we cut our search space in half each time. This would take about 30 steps This is an O(log(n)) algorithm

Example: crawler Now we want to make a crawler method that will print out files and directories Java provides us with a File object http://java.sun.com/javase/6/docs/api/java/io/File.html Given the file or directory we want, we can print its name public static void print(File f) { System.out.println(f.getName()); } We also want to print the contents of a directory, though

Example: crawler We can get an array of the files in the directory and print their names: public static void print(File f) { System.out.println(f.getName()); for (File subF : f.listFiles()) { System.out.println(“ “+subF.getName()); } } But this only works if f is a directory, otherwise it throws a NullPointerException

Example: crawler So we check to make sure f is a directory first: public static void print(File f) { System.out.println(f.getName()); if ( f.isDirectory() ) { for (File subF : f.listFiles()) { System.out.println(" “+subF.getName()); } } } But this still only works for one level of directories

Example: crawler So we check to make sure f is a directory first: public static void print(File f) { System.out.println(f.getName()); if ( f.isDirectory() ) { for (File subF : f.listFiles()) { System.out.println(" “+subF.getName()); if ( subF.isDirectory() ) { for (File subSubF : subF.listFiles() ) { ... } } } } } Redundant! And it still only works for one more level of directories

Example: crawler If only we had a method that would print the files in a directory...but we do! public static void print(File f) { System.out.println(f.getName()); if ( f.isDirectory() ) { for (File subF : f.listFiles()) { print(subF); } } } We probably want to add indentation, as well

Example: crawler So we add a String parameter to show how much each file name should be indented public static void print(File f, String indent) { System.out.println(indent + f.getName()); if ( f.isDirectory() ) { for (File subF : f.listFiles()) { print(subF, indent + " "); } } } We don’t want our client to need to pass a String, though

Example: crawler Instead, we make a public method for the client and hide the extra parameter by making our other method private: public static void print(File f) { print(f, ""); } private static void print(File f, String indent) { System.out.println(indent + f.getName()); if ( f.isDirectory() ) { for (File subF : f.listFiles()) { print(subF, indent + " "); } } } Using a public/private pair is common in recursion when we want to work with extra parameters

Example: Sierpinski Chapter 12 in the book discusses the Sierpinski triangle, a fractal (recursive image):