Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved.

Slides:



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

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
What’s in a Language naming objects (variables) naming processes (methods/functions) making decisions (if) making repetitions (for, while) (recursion)
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: 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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
Problem Solving 4 Algorithms, Problem Solving and Recursion ICS-201 Introduction to Computing II Semester 071.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
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:
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
1Recursion. 2 Outline thinking recursively recursive algorithms iteration vs. recursion recursive functions integer exponentiation (pow) infinite recursion.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
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 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Recursion Copyright (c) Pearson All rights reserved.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Recursion. 2 recursion: The definition of an operation in terms of itself. –Solving a problem using recursion depends on solving smaller occurrences of.
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Recursion.
CS314 – Section 5 Recitation 9
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion Version 1.0.
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Lecture 9: introduction to recursion reading: 12.1
Recursion -- Introduction
CprE 185: Intro to Problem Solving (using C)
Lecture 10: recursive programming reading:
Building Java Programs
Recursion DRILL: Please take out your notes on Recursion
More on Recursion.
Recursion
Building Java Programs
Recursion Copyright (c) Pearson All rights reserved.
Applied Algorithms (Lecture 17) Recursion Fall-23
slides adapted from Marty Stepp and Hélène Martin
Java Strings Slides provided by the University of Washington Computer Science & Engineering department.
Chapter 12 Recursion (methods calling themselves)
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Lecture 10: recursive programming reading:
CSE 143 Lecture 11 Recursive Programming reading:
Building Java Programs
Recursion.
CSE 143 Lecture 11 Recursive Programming slides created by Marty Stepp
slides created by Marty Stepp and Alyssa Harding
Recursion based on slides by Alyssa Harding
Topic 6 loops, figures, constants
Recursion Problems.
Recursion Based on slides by Alyssa Harding
Lecture 14: Strings and Recursion AP Computer Science Principles
Building Java Programs
CSE 143 Lecture 13 Recursive Programming reading:
slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 13 Recursive Programming reading:
Building Java Programs
slides created by Marty Stepp
Building Java Programs
Recursive Thinking.
Presentation transcript:

Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved.

Recursion recursion: The definition of an operation in terms of itself. Solving a problem using recursion depends on solving smaller occurrences of the same problem. recursive programming: Writing methods that call themselves to solve problems recursively. An equally powerful substitute for iteration (loops) Particularly well-suited to solving certain types of problems

Why learn recursion? "cultural experience" - A different way of thinking of problems Can solve some kinds of problems better than iteration Leads to elegant, simplistic, short code (when used well) Many programming languages ("functional" languages such as Scheme, ML, and Haskell) use recursion exclusively (no loops)

Recursion and cases Every recursive algorithm involves at least 2 cases: base case: A simple occurrence that can be answered directly. recursive case: A more complex occurrence of the problem that cannot be directly answered, but can instead be described in terms of smaller occurrences of the same problem. Some recursive algorithms have more than one base or recursive case, but all have at least one of each. A crucial part of recursive programming is identifying these cases.

Example You are lined up in front of your favorite store for Black Friday deals. The line is long and wraps around the building so that you cannot see the front of the line. How do you figure out your position without getting out of line? Answer: Ask the person in front of you. Base case: If a customer is at the front of the line and someone asks him for his position, he’ll “return” 1. Recursive case: If a customer is at position n and someone asks him for his position, he’ll ask the person in front of him. Note: The recursive case reduces an n problem to an n-1 problem.

Recursion in Java Consider the following method to print a line of * characters: // Prints a line containing the given number of stars. // Precondition: n >= 0 public static void printStars(int n) { for (int i = 0; i < n; i++) { System.out.print("*"); } System.out.println(); // end the line of output Write a recursive version of this method (that calls itself). Solve the problem without using any loops. Hint: Your solution should print just one star at a time.

"Recursion Zen" The real, even simpler, base case is an n of 0, not 1: public static void printStars(int n) { if (n == 0) { // base case; just end the line of output System.out.println(); } else { // recursive case; print one more star System.out.print("*"); printStars(n - 1); } Recursion Zen: The art of properly identifying the best set of cases for a recursive algorithm and expressing them elegantly.

Exercise Write a recursive method pow accepts an integer base and exponent and returns the base raised to that exponent. Example: pow(3, 4) returns 81 Solve the problem recursively and without using loops.

pow solution // Returns base ^ exponent. // Precondition: exponent >= 0 public static int pow(int base, int exponent) { if (exponent == 0) { // base case; any number to 0th power is 1 return 1; } else { // recursive case: x^y = x * x^(y-1) return base * pow(base, exponent - 1); }

Recursive tracing Consider the following recursive method: public static int mystery(int n) { if (n < 10) { return n; } else { int a = n / 10; int b = n % 10; return mystery(a + b); } What is the result of the following call? mystery(648)

A recursive trace mystery(648): int a = 648 / 10; // 64 int b = 648 % 10; // 8 return mystery(a + b); // mystery(72) mystery(72): int a = 72 / 10; // 7 int b = 72 % 10; // 2 return mystery(a + b); // mystery(9) mystery(9): return 9;

Recursive tracing 2 Consider the following recursive method: public static int mystery(int n) { if (n < 10) { return (10 * n) + n; } else { int a = mystery(n / 10); int b = mystery(n % 10); return (100 * a) + b; } What is the result of the following call? mystery(348)

A recursive trace 2 mystery(348) int a = mystery(34); return (10 * 3) + 3; // 33 int b = mystery(4); return (10 * 4) + 4; // 44 return (100 * 33) + 44; // 3344 int b = mystery(8); return (10 * 8) + 8; // 88 return (100 * 3344) + 88; // 334488 What is this method really doing?

Recursive trace 3 int mystery(int n){ if (n == 1 || n == 2) return 2*n; else return mystery(n-1) - mystery(n-2); } What is the result of the following call? mystery(4) Answer: -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 { String first = s.substring(0,1); String last = s.substring(s.length() - 1); if (!first.equals(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.substring(0,1).equals(s.substring(s.length()-1)) && isPalindrome(s.substring(1, s.length() - 1));

Lab 1: Fractal Circles Use Processing to write the recursive method to print out a recursive pattern of circles of decreasing radii. void circle(int x, int radius, int depth) {…} The call circle(width/2, width/4, 10) should produce the image on the following slide. Use noFill() before drawing the circle to make it transparent.

Lab 1

Lab 2: Sierpinski Triangle Use Processing to write the recursive method to the Sierpinski triangle. void fractalTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int n) {…}

Lab 2