Lecture 18 Recursion part 2 Richard Gesick.

Slides:



Advertisements
Similar presentations
Lecture 12 Recursion part 1
Advertisements

Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful:  for example, in.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
CHAPTER 17 RECURSION CHAPTER GOALS –To learn about the method of recursion –To understand the relationship between recursion and iteration –To analysis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Recursion.
Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms.
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.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Lecture 13 Recursion part 2 Richard Gesick. Advanced Recursion Sometimes recursion involves processing a collection. While it could be done using 'foreach'
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
IB Computer Science Unit 5 – Advanced Topics Recursion.
CSC 212 More Recursion, Stacks, & Queues. Linear Recursion Test for the bases cases  At least one base case needs to be defined If not at a base case,
Problem Set 5: Problem 2 22C:021 Computer Science Data Structures.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
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.
CPS Today’s topics Programming Recursion Invariants Reading Great Ideas, p Brookshear, Section Upcoming Copyrights, patents, and.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Introduction to Recursion
5.13 Recursion Recursive functions Functions that call themselves
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.
Computer Science 4 Mr. Gerb
Sum of natural numbers class SumOfNaturalNumbers {
Recursion Chapter 12.
CHAPTER 7 RECURSIVE FUNCTION
CS Week 14 Jim Williams, PhD.
Lecture 11 C Parameters Richard Gesick.
Recursion: The Mirrors
Lecture 17 Recursion part 1 Richard Gesick.
Recursion Chapter 18.
Recursion based on slides by Alyssa Harding
Loops.
From Recursion To Iteration: A Case Study
Java Programming: Chapter 9: Recursion Second Edition
Lecture 13 Recursion part 2 CSE /26/2018.
Lecture 12 Recursion part 1 CSE /26/2018.
Fundaments of Game Design
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion Method calling itself (circular definition)
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
More on iterations using
Ch 12 Recursion Mr. Dave Clausen La Cañada High School
Presentation transcript:

Lecture 18 Recursion part 2 Richard Gesick

Advanced Recursion Sometimes recursion involves processing a collection.  While it could be done using 'foreach' or some other iterative approach, recall that recursion works best for non-linear structures.  Recursion is also useful for making use of the activation stack as a "reminder" of where we've been and what remains to be done.

multiple recursion Consider this example: what is DoWork(5) ? void DoWork(int i) {   Console.WriteLine(i);   if (i > 2)   {     DoWork(i-1);     DoWork(i-2);   }   Console.WriteLine(i); }

multiple recursion (2) Fibonacci series: an= a(n-1) +a(n-2) i.e. the next number in the series is the sum of the 2 preceding numbers. the Fibonacci series: 1,1,2,3,5,8,13,… Write a multiply recursive method: int Fibo ( int n) { } (Remember stopping state first!)

The solution int Fib(int i) {   if (i < 2)     return 1;   else     return Fib(i-1) + Fib(i-2); }

How can we reverse a string recursively? String Processing How can we reverse a string recursively?

Pseudo-code solution (base case is when string is ? ): reverse a string by repeatedly removing the first character, reversing this substring (recursively), appending the first character to the end

string reversal code static string Reverse (string s) {   if (s.Length==1)     return s;   else     return Reverse(s.Substring(1)) + s[0]; }

Non-linear Recursion Folders and files within a computer system are an excellent example of a non-linear structure that is easily processed via recursion.  This is also possible using iteration, but we would have more coding work to do in handling where we've been in the file system.  We can leverage the activation stack to remember for us.

Non-linear Recursion Consider the steps to count the total number of files: If we're in a folder with no files (or subfolders), then we return 0 Otherwise, we return the count of the files in the folder + all the files in the subfolders It's the second part that's tricky... but notice that's where the recursive call comes in.  We just need to recursively call for each subfolder (so this is recursion wrapped inside of iteration):

solution private int CountFiles(DirectoryInfo di) { int file_count = 0; file_count += di.GetFiles().Length; foreach (DirectoryInfo sub_folder in di.GetDirectories())     file_count += CountFiles(sub_folder); return file_count; }