1 CS100J February 23 Recursion Recursion: If you get the point, stop; otherwise, see Recursion. Infinite recursion: See Infinite recursion. Read: pp. 403-408.

Slides:



Advertisements
Similar presentations
Good Morning Class. The cat needs some water. The dog wants a bone. That bird lives in a birdhouse where she feels quite at home. How do people take care.
Advertisements

Wilson Reading Decodable Poems
Spelling  In first grade, students will begin having spelling homework and a spelling test on Fridays starting the week of September 16th. Each week we.
Recursion in Python. Recursion Problems in every area of life can be defined recursively, that is, they can be described in terms of themselves. An English.
1 CS1110 Lec Oct 2010 More on Recursion We develop recursive functions and look at execution of recursive functions Study Sect 15.1, p Watch.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Recursion. Binary search example postponed to end of lecture.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion.
1 CS100J 14 April 2005 Exceptions in Java. Read Chapter 10. This material will not be tested Review session Sunday in Olin 155, 1PM-3PM. I believe it is.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
RECURSION Lecture 7 CS2110 – Spring Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
Recursion James Wright St Peter’s Secondary School 733 Parkhill Road West Peterborough,Ontario K9J-8M4
Microbial Genetics I. Structure and function of genes II. Gene expression in bacteria III. Mutation and variation IV. Horizontal gene transfer in bacteria.
1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:
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.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
RECURSION Lecture 7 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion in C++. Recursion Recursive tasks: A task that is defined in terms of itself. A function that calls itself. With each invocation, the problem.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
1 CS1110 October 02. Recursion Recursion: If you get the point, stop; otherwise, see Recursion. Infinite recursion: See Infinite recursion. Read: pp
1 Recursion: If you get the point, stop; otherwise, see Recursion. Infinite recursion: See Infinite recursion. Read: pp but SKIP sect
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Reading Warm up.
1 CS100J 26 Feb 2008 More on Recursion My first job was working in an orange juice factory, but I got canned: couldn't concentrate. Then I worked in the.
while there is room A draws or ; B draws or ; A wants to get a solid closed curve. B wants to stop A from getting a solid closed curve. Who.
Rhyming Words cat cop shirt fox dog car skirt top box fog bat jar.
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
RECURSION, CONTINUED Lecture 8 CS2110 – Fall 2015.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
RECURSION Lecture 7 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
23 February Recursion and Logarithms CSE 2011 Winter 2011.
Review of Recursion  a recursive method calls itself  to prevent infinite recursion you need to ensure that: 1. the method reaches a base case 2. each.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
1 Recursion and induction We teach these early, instead of new object- oriented ideas, so that those who are new to Java can have a chance to catch up.
RECURSION Lecture 8 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
CS 100Lecture 201 Announcements P5 due Thursday Final exam: Tuesday August 10th, 8AM, Olin 155, 2 hours Tomorrow, Wednesday: C, pointers Thursday, Friday:
CSC 205 Programming II Lecture 8 Recursion.
Identify the Appropriate Method for Handling Repetition
Written and illustrated by: Russell Benfanti Genre: Fantasy
Recursion (Continued)
Chapter Topics Chapter 16 discusses the following main topics:
Recursion -- Introduction
Data Structures with Java © Rick Mercer
Chapter 15 Recursion.
Start reading Sec and chapter 7 on loops
Written and Photographed by: Nina Crews Genre: Informational text
Short a sound listening exercises
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Recursion: If you get the point, stop; otherwise, see Recursion.
Recursion Lecture 8 CS2110 – Spring 2018.
Testing change to a linked list
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
CS1110 Lec Oct 2010 More on Recursion
Recursion Lecture 8 CS2110 – Fall 2018.
It’s turtles all the way down
Dr. Sampath Jayarathna Cal Poly Pomona
CS100A Lecture 25 1 December 1998 Chance to replace grade on Prelim 3, Question 2. Everyone is expected to be in lecture on Thursday, 3 December.
Download presented algorithms from the website
Recursive Thinking.
Grand Letter Segmentation #8 short /a/ words
Presentation transcript:

1 CS100J February 23 Recursion Recursion: If you get the point, stop; otherwise, see Recursion. Infinite recursion: See Infinite recursion. Read: pp but SKIP sect Look in ProgramLive CD, page 15-3, for some interesting recursive methods. Download presented algorithms from the website Recursive definition: A definition that is defined in terms of itself. Recursive method: a method that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops), which we cover next. Recursion is an important programming tool. Functional languages have no loops —only recursion.

2 Turn recursive definition into recursive function Factorial: !0 = 1base case !n = n * !(n-1) for n > 0recursive case Thus, !3 = 3 * !2 = 3 * 2 * !1 = 3 * 2 * 1 * !0 = 3 * 2 * 1 * 1 (= 6) // = !n (for n ≥ 0) public static int fact(int n) { if (n == 0) { return 1; base case } // {n > 0}an assertion return n * fact(n-1); recursive case }(a recursive call) note the precise specification

3 Two issues in coming to grips with recursion 1. How are recursive calls executed? 2. How do we understand a recursive method and how do we create one? We discuss the first issue later. Suffice it to say that if you execute a call on a recursive method, carefully using our model of execution, you will see that it works. Briefly, a new frame is created for each recursive call. DON’T try to understand a recursive method by executing its recursive calls! Use execution only to understand how it works.

4 Understanding a recursive method Factorial: !0 = 1 base case !n = n * !(n-1) for n > 0recursive case Step 1: HAVE A PRECISE SPECIFICATION // = !n (for n ≥ 0) public static int fact(int n) { if (n == 0) { return 1; base case } // {n > 0} return n * fact(n-1); recursive case }(a recursive call) Step 2: Check the base case. When n = 0, 1 is returned, which is 0!. So the base case is handled correctly.

5 Understanding a recursive function Factorial: !0 = 1 base case !n = n * !(n-1) for n > 0recursive case Step 3: Recursive calls make progress toward termination. /** = !n (for n>=0) */ public static int fact(int n) { if (n == 0) { return 1; } // {n > 0} return n * fact(n-1); recursive case } argument n-1 is smaller than parameter n, so there is progress toward reaching base case 0 parameter n argument n-1 Step 4: Recursive case is correct.

6 Creating a recursive method Task: Write a method that removes blanks from a String. 0. Specification: /** = s but with its blanks removed */ public static String deblank(String s) 1. Base case: the smallest String is “”. if (s.length() == 0) return s; 2. Other cases: String s has at least 1 character. If it’s blank, return s[1..] but with its blanks removed. If it’s not blank, return s[0] + (s[1..] but with its blanks removed) Notation: s[i] is shorthand for s.charAt[i]. s[i..] is shorthand for s.substring(i). precise specification!

7 Creating a recursive method // = s but with its blanks removed public static String deblank(String s) { if (s.length() == 0) return s; // {s is not empty} if (s[0] is a blank) return s[1..] with its blanks removed // {s is not empty and s[0] is not a blank} return s[0] + (s[1..] with its blanks removed); } The tasks given by the two English, blue expressions are similar to the task fulfilled by this function, but on a smaller String! !!!Rewrite each as deblank(s[1..]). Notation: s[i] is shorthand for s.charAt[i]. s[i..] is shorthand for s.substring(i).

8 Creating a recursive method // = s but with its blanks removed public static String deblank(String s) { if (s.length == 0) return s; // {s is not empty} if (s.charAt(0) is a blank) return deblank(s.substring(1)); // {s is not empty and s[0] is not a blank} return s.charAt(0) + deblank(s.substring(1)); } Check the four points: 0. Precise specification? 1. Base case: correct? 2. Recursive case: progress toward termination? 3. Recursive case: correct?

9 Check palindrome-hood A String with at least two characters is a palindrome if (0) its first and last characters are equal, and (1) chars between first & last form a palindrome: e.g. AMANAPLANACANALPANAMA /** = “s is a palindrome” */ public static boolean isPal(String s) { if (s.length() <= 1) return true; // { s has at least two characters } return s.charAt(0) == s.charAt(s.length()-1) && isPal(s.substring(1, s.length()-1)); } have to be the same has to be a palindrome

10 A man, a plan, a caret, a ban, a myriad, a sum, a lac, a liar, a hoop, a pint, a catalpa, a gas, an oil, a bird, a yell, a vat, a caw, a pax, a wag, a tax, a nay, a ram, a cap, a yam, a gay, a tsar, a wall, a car, a luger, a ward, a bin, a woman, a vassal, a wolf, a tuna, a nit, a pall, a fret, a watt, a bay, a daub, a tan, a cab, a datum, a gall, a hat, a fag, a zap, a say, a jaw, a lay, a wet, a gallop, a tug, a trot, a trap, a tram, a torr, a caper, a top, a tonk, a toll, a ball, a fair, a sax, a minim, a tenor, a bass, a passer, a capital, a rut, an amen, a ted, a cabal, a tang, a sun, an ass, a maw, a sag, a jam, a dam, a sub, a salt, an axon, a sail, an ad, a wadi, a radian, a room, a rood, a rip, a tad, a pariah, a revel, a reel, a reed, a pool, a plug, a pin, a peek, a parabola, a dog, a pat, a cud, a nu, a fan, a pal, a rum, a nod, an eta, a lag, an eel, a batik, a mug, a mot, a nap, a maxim, a mood, a leek, a grub, a gob, a gel, a drab, a citadel, a total, a cedar, a tap, a gag, a rat, a manor, a bar, a gal, a cola, a pap, a yaw, a tab, a raj, a gab, a nag, a pagan, a bag, a jar, a bat, a way, a papa, a local, a gar, a baron, a mat, a rag, a gap, a tar, a decal, a tot, a led, a tic, a bard, a leg, a bog, a burg, a keel, a doom, a mix, a map, an atom, a gum, a kit, a baleen, a gala, a ten, a don, a mural, a pan, a faun, a ducat, a pagoda, a lob, a rap, a keep, a nip, a gulp, a loop, a deer, a leer, a lever, a hair, a pad, a tapir, a door, a moor, an aid, a raid, a wad, an alias, an ox, an atlas, a bus, a madam, a jag, a saw, a mass, an anus, a gnat, a lab, a cadet, an em, a natural, a tip, a caress, a pass, a baronet, a minimax, a sari, a fall, a ballot, a knot, a pot, a rep, a carrot, a mart, a part, a tort, a gut, a poll, a gateway, a law, a jay, a sap, a zag, a fat, a hall, a gamut, a dab, a can, a tabu, a day, a batt, a waterfall, a patina, a nut, a flow, a lass, a van, a mow, a nib, a draw, a regular, a call, a war, a stay, a gam, a yap, a cam, a ray, an ax, a tag, a wax, a paw, a cat, a valley, a drib, a lion, a saga, a plat, a catnip, a pooh, a rail, a calamus, a dairyman, a bater, a canal ---Panama!

11 Hilbert’s space-filling curve Hilbert(1): Hilbert(2): Hilbert(n): H(n-1) As the size of each line gets smaller and smaller, in the limit, this algorithm fills every point in space. Lines never overlap.

12 Hilbert’s space-filling curve