Recursion occurs when a method calls itself. public class RecursionOne { public void run(int x) { System.out.println(x); run(x+1); } public static void.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
1 MATERI PENDUKUNG JUMP Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Recursion.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 CS 177 Week 5 Recitation Slides Loops. 2 Announcements Project 2 due next Thursday at 9PM. Exam 1 this evening (switch and loop not covered) Old exams.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
1 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Methods We write methods in our programs for many reasons:
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Subprograms - implementation
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Java Methods 11/10/2015. Learning Objectives  Be able to read a program that uses methods.  Be able to write a write a program that uses methods.
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.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Recursion occurs when a method calls itself. Google “recursion”
Function Recursion to understand recursion you must understand recursion.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
to understand recursion you must understand recursion
Topic 6 Recursion.
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Recursion Review Mr. Jacobs.
Control Structures.
10.2 Implementation and Execution of Recursive Code
Advanced Programming in Java
to understand recursion you must understand recursion
Multithreaded Programming in Java
Recursion Output Input
Call Stacks, Arguments and Returns
Starting Out with Java: From Control Structures through Objects
Lecture 17 Recursion part 1 Richard Gesick.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
class PrintOnetoTen { public static void main(String args[]) {
Basics of Recursion Programming with Recursion
slides created by Alyssa Harding
Scope of variables class scopeofvars {
Lecture 12 Recursion part 1 CSE /26/2018.
The structure of programming
Fundaments of Game Design
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Recursion Method calling itself (circular definition)
A Methodical Approach to Methods
Methods/Functions.
Presentation transcript:

Recursion occurs when a method calls itself.

public class RecursionOne { public void run(int x) { System.out.println(x); run(x+1); } public static void main(String args[] ) { RecursionOne test = new RecursionOne(); test.run(1); } Will it stop? OUTPUT stack overflow

A recursive method must have a stop condition/ base case. Recursive calls will continue until the stop condition is met.

public class RecursionTwo { public void run(int x ) { System.out.println(x); if(x<5) run(x+1); } public static void main(String args[] ) { RecursionTwo test = new RecursionTwo(); test.run(1); } } base case It will stop! OUTPUT

public class RecursionThree { public void run(int x ) { if(x<5) run(x+1); System.out.println(x); } public static void main(String args[] ) { RecursionThree test = new RecursionThree (); test.run(1); } } base case OUTPUT

Recursion is basically a loop that is created using method calls.

class DoWhile { public void run( ) { int x=0; while(x<10) { x++; System.out.println(x); } public static void main(String args[] ) { DoWhile test = new DoWhile(); test.run( ); }

When you call a method, an activation record for that method call is put on the stack and it keeps track of all parameters/ arguments being passed, as well as what the method returns.

AR1- method() call Because AR1 is placed on the stack first, it will be the last AR removed from the stack.

AR1- method() call AR2- method() call Because AR2 is placed on the stack second, it will be the second to last AR removed from the stack.

AR1- method() call AR2- method() call AR3- method() call

AR1- method() call AR2- method() call AR3- method() call AR4- method() call AR4 is placed on the stack last and it is processed to completion first. Once AR4 is finished, the execution sequence returns to AR3.

AR1- method() call AR2- method() call AR3- method() call

AR1- method() call AR2- method() call

AR1- method() call As each call to the method completes, the instance of that method is removed from the stack. Because AR1 was placed on the stack first, it is processed to completion last.

public class RecursionTwo { public void run(int x ) { System.out.println(x); if(x<5) run(x+1); } public static void main(String args[] ) { RecursionTwo test = new RecursionTwo(); test.run(1); } OUTPUT Because the println(x) is before the recursive call, x is printed before the next AR is created on the stack.

public class RecursionThree { public void run(int x ) { if(x<5) run(x+1); System.out.println(x); } public static void main(String args[] ) { RecursionThree test = new RecursionThree(); test.run(1); } OUTPUT Why does this output differ from RecursionTwo? Because the println(x) is after the recursive call, the println(x) is delayed until the new AR is completed. The println(x) will happen when the AR above it has finished.

int fun(int y) { if(y<=1) return 1; else return fun(y-2) + y; } //test code in client class System.out.println(test.fun(5)); AR3 y 1 return 1 AR2 y 3 return AR3 + 3 AR1 y 5 return AR

int fun( int x, int y) { if( y < 1) return x; else return fun( x, y - 2) + x; } //test code in client class out.println(test.fun(4,3)); AR3 x y 4 -1 return 4 AR2 x y 4 1 return AR3 + 4 AR1 x y 4 3 return AR

int fun(int x, int y) { if ( x == 2 ) return x; else return x+fun(y-1,x); } What would fun(4,4) return? OUTPUT 12

System.out.println(recur("abc")); public String recur(String s) { int len = s.length(); if(len>0) return s.substring(len-1) + recur(s.substring(0,len-1)); return ""; }

call System.out.println(recur("abc")) AR1 : s = "abc" return "c" + AR2 AR stands for activation record. An AR is placed on the stack every time a method is called.

AR2 : s = "ab" return "b" + AR3 AR1 : s = "abc" return "c" + AR2

AR3 : s = "a" return "a" + AR4 AR2 : s = "ab" return "b" + AR3 AR1 : s = "abc" return "c" + AR2

AR4 : s = "" return "" AR3 : s = "a" return "a" + AR4 AR2 : s = "ab" return "b" + AR3 AR1 : s = "abc" return "c" + AR2

AR3 : s = "a" return "a" AR2 : s = "ab" return "b" + AR3 AR1 : s = "abc" return "c" + AR2

AR2 : s = "ab" return "ba" AR1 : s = "abc" return "c" + AR2

OUTPUT cba System.out.println(recur("abc")); AR1 : s = "abc" return "cba"

If recursion is just a loop, why would you just not use a loop? Recursion is a way to take a block of code and spawn copies of that block over and over again. This helps break a large problem down into smaller pieces.

If checking 0 0, you would find are @ @ at spot at spot at spot at spot at spot [1,2] The exact same checks are made at each spot.

if ( r and c are in bounds and current spot is ) mark spot as visited bump up current count by one countAts up countAts down countAts left countAts right This same block of code is recreated with each recursive call. The exact same code is used to check many different locations.

if ( r and c are in bounds and current spot is ) mark spot as visited bump up current count by one countAts up countAts down countAts left countAts @ 0 @ 0 -