CSE 143 Lecture 11 Recursive Programming slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
Advertisements

CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Recursion.
When confronted with a new problem there are two questions you should ask: 1. Is this problem like, or a special case of, a problem that I already know.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
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:
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
Chapter 8 Recursion Modified.
CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 20 Recursion.
CSE 143 Lecture 13 Recursive Programming reading: slides created by Marty Stepp
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
RECURSION, CONTINUED Lecture 8 CS2110 – Fall 2015.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion.
CSE 143 Lecture 14 Maps/Sets; Grammars reading: slides adapted from Marty Stepp and Hélène Martin
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Recursion Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Chapter 18 Recursion CS1: Java Programming Colorado State University
Chapter 14 Recursion.
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Lecture 10: recursive programming reading:
Chapter 19 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Programming: Program Design Including Data Structures
Building Java Programs
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Building Java Programs
Recursive Definitions
Building Java Programs
CSE 341 Lecture 3 let expressions; pattern matching Ullman
slides adapted from Marty Stepp
Lecture 10: recursive programming reading:
CSE 143 Lecture 14 (A) More Recursive Programming
CSE 143 Lecture 11 Recursive Programming reading:
CSE 143 Lecture 2 Collections and ArrayIntList
Building Java Programs
Building Java Programs
CSE 143 Lecture 14 More Recursive Programming; Grammars
Recursion Chapter 18.
CSE 143 Lecture 4 Implementing ArrayIntList
Recursion Problems.
Chapter 17 Recursion.
Chapter 18 Recursion.
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.
CSE 143 Lecture 18 More Recursive Backtracking
slides created by Alyssa Harding
CSE 143 Lecture 3 Implementing ArrayIntList reading:
Suggested self-checks: Section 7.11 #1-11
CSE 143 Lecture 13 Recursive Programming reading:
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
CSE 143 Lecture 13 Recursive Programming reading:
Building Java Programs
CSE 143 Lecture 2 ArrayIntList, binary search
CSE 143 Lecture 18 More Recursive Backtracking
Lecture 10: recursive programming reading:
Lecture 22: Number Systems
slides created by Alyssa Harding
Implementing ArrayIntList
Presentation transcript:

CSE 143 Lecture 11 Recursive Programming slides created by Marty Stepp http://www.cs.washington.edu/143/

Decimal Numbers A visual review of decimal numbers: 348 = 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

Binary Numbers Binary is exactly the same, but base 2 Decimal Binary 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

Exercise Write a recursive method printBinary that accepts an integer and prints that number's representation in binary (base 2). Example: printBinary(7) prints 111 Example: printBinary(12) prints 1100 Example: printBinary(42) prints 101010 Write the method recursively and without using any loops. place 10 1 32 16 8 4 2 value

Case analysis Recursion is about solving a small piece of a large problem. What is 69743 in binary? Do we know anything about its representation in binary? Case analysis: What is/are easy numbers to print in binary? Can we express a larger number in terms of a smaller number(s)? Suppose we are examining some arbitrary integer N. if N's binary representation is 10010101011 (N / 2)'s binary representation is 1001010101 (N % 2)'s binary representation is 1

printBinary solution // Prints the given integer's binary representation. // Precondition: n >= 0 public static void printBinary(int n) { if (n < 2) { // base case; same as base 10 System.out.println(n); } else { // recursive case; break number apart printBinary(n / 2); printBinary(n % 2); } Can we eliminate the precondition and deal with negatives?

printBinary solution 2 // Prints the given integer's binary representation. public static void printBinary(int n) { if (n < 0) { // recursive case for negative numbers System.out.print("-"); printBinary(-n); } else if (n < 2) { // base case; same as base 10 System.out.println(n); } else { // recursive case; break number apart printBinary(n / 2); printBinary(n % 2); }

Exercise Write a recursive method isPalindrome accepts a String and returns true if it reads the same forwards as backwards. isPalindrome("madam")  true isPalindrome("racecar")  true isPalindrome("step on no pets")  true isPalindrome("able was I ere I saw elba")  true isPalindrome("Java")  false isPalindrome("rotater")  false isPalindrome("byebye")  false isPalindrome("notion")  false

Exercise solution // Returns true if the given string reads the same // forwards as backwards. // Trivially true for empty or 1-letter strings. public static boolean isPalindrome(String s) { if (s.length() < 2) { return true; // base case } else { char first = s.charAt(0); char last = s.charAt(s.length() - 1); if (first != last) { return false; } // recursive case String middle = s.substring(1, s.length() - 1); return isPalindrome(middle); }

Exercise solution 2 // Returns true if the given string reads the same // forwards as backwards. // Trivially true for empty or 1-letter strings. public static boolean isPalindrome(String s) { if (s.length() < 2) { return true; // base case } else { return s.charAt(0) == s.charAt(s.length() - 1) && isPalindrome(s.substring(1, s.length() - 1)); }

Exercise Write a method crawl accepts a File parameter and prints information about that file. If the File object represents a normal file, just print its name. If the File object represents a directory, print its name and information about every file/directory inside it, indented. cse143 handouts syllabus.doc lecture_schedule.xls homework 1-sortedintlist ArrayIntList.java SortedIntList.java index.html style.css recursive data: A directory can contain other directories.

File objects A File object (from the java.io package) represents a file or directory on the disk. Constructor/method Description File(String) creates File object representing file with given name canRead() returns whether file is able to be read delete() removes file from disk exists() whether this file exists on disk getName() returns file's name isDirectory() returns whether this object represents a directory length() returns number of bytes in file listFiles() returns a File[] representing files in this directory renameTo(File) changes name of file

Public/private pairs We cannot vary the indentation without an extra parameter: public static void crawl(File f, String indent) { Often the parameters we need for our recursion do not match those the client will want to pass. In these cases, we instead write a pair of methods: 1) a public, non-recursive one with the parameters the client wants 2) a private, recursive one with the parameters we really need

Exercise solution // Prints information about this file, // and (if it is a directory) any files inside it. public static void crawl(File f) { crawl(f, ""); // call private recursive helper } // Recursive helper to implement crawl/indent behavior. private static void crawl(File f, String indent) { System.out.println(indent + f.getName()); if (f.isDirectory()) { // recursive case; print contained files/dirs for (File subFile : f.listFiles()) { crawl(subFile, indent + " ");

Sum Write a method sum that takes an integer array as a parameter and computes the sum of the array elements recursively. // returns the sum of the numbers in the given array public static int sum(int[] list) { return sum(list, 0); } // computes the sum of the list starting at the given index private static int sum(int[] list, int index) { if (index == list.length) { return 0; } else { return list[index] + sum(list, index + 1); } }

Towers of Hanoi

Towers of Hanoi public class Towers { public static void main(String[] args) { towers(3, "A", "C", "B"); } public static void towers(int num, String from, String to, String free) { if (num == 1) { System.out.println("Move " + num + " from " + from + " to " + to); } else { towers(num - 1, from, free, to); System.out.println("Move " + num + " from " + from + " to " + to); towers(num - 1, free, to, from); } } }

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