Download presentation
Presentation is loading. Please wait.
1
Lecture 13 Recursion part 2 CSE 1322 4/26/2018
2
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. 4/26/2018
3
multiple recursion Consider this example: what is DoWork(4) ?
void DoWork(int i) { Console.WriteLine(i); if (i > 2) { DoWork(i-1); DoWork(i-2); } Console.WriteLine(i); } void DoWork(int i) { System.out.println(i); if (i > 2) { DoWork(i-1); DoWork(i-2); } System.out.println(i); } 4/26/2018
4
Java - multiple recursion Consider this example: what is DoWork(4) ?
4/26/2018
5
C# - multiple recursion Consider this example: what is DoWork(4) ?
4/26/2018
6
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!) 4/26/2018
7
The solution int Fib(int i) { if (i <= 2) return 1; else return Fib(i-1) + Fib(i-2); } 4/26/2018
8
Java - multiple recursion Consider this example: what is Fibonacci(?) ?
4/26/2018
9
C# - multiple recursion Consider this example: what is Fibonacci(?) ?
4/26/2018
10
How can we reverse a string recursively?
String Processing How can we reverse a string recursively? 4/26/2018
11
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 4/26/2018
12
string reversal code public string Reverse (string s) { if (s.Length==1) return s; else return Reverse(s.Substring(1)) + s[0]; } 4/26/2018
13
string reversal code public String Reverse (String s) { if (s.length()==1) return s; else return Reverse(s.substring(1)) + s.charAt(0); } 4/26/2018
14
string reversal Complete Program
4/26/2018
15
string reversal Complete Program
4/26/2018
16
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. 4/26/2018
17
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): 4/26/2018
18
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; } 4/26/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.