Title: CSCI 240 Abstract Data Types
1Department of Computer and Information
Science,School of Science, IUPUI
CSCI 240
Review of Recursion For those of you that have
slept since then
Dale Roberts, Lecturer IUPUI droberts_at_cs.iupui.edu
2Why Recursion?
- If sequence, ifs, and loops are enough, why
recursion? - Subset of looping problems, where the problem
breaks down into identical pieces - We will use a series of problems with
straightforward recursive solutions to motivate
our discussion. - Practical example? Directory Search!
3Introduction to Recursion
- Problem based approach
- A simple example factorial
- Simple definition of recursion
- Bridges Parallels with looping
- See if you can do it GCD
- A realistic example Directory Search
- Conclusion
4Simple Example Factorial
- An easy first example is to consider the problem
of implementing a function that calculates a
factorial. - Recall from math that factorial is defined as
0! 1 n! n (n-1)! where n is not
negative.
Base Case
Recursive Case
5Implementation
- int factorial(int N)
-
- if (N 0)
- return 1
- return Nfactorial(N-1)
-
Base Case
Recursive Case
Sedgewick Program 5.1
6Factorial Sample Execution
iFactorial(4) 4iFactorial(3) iFactorial(3)
3iFactorial(2) iFactorial(2) 2iFactorial(1) iFa
ctorial(1) 1iFactorial(0) iFactorial(0) 1
iFactorial(4) 46 24 iFactorial(3) 32
6 iFactorial(2) 21 2 iFactorial(1) 11
1 iFactorial(0) 1
Surface Phase
Dive Phase
Maximum Depth 5
7A simple definition of recursion
- Recursion simply means a function that calls
itself. - In order to keep the recursion from going on
forever, you must make sure you hit a termination
condition called the base case. - The number of nested invocations is called the
depth of recursion. - Function may call itself directly or indirectly.
(All of our examples are direct.)
8Questionable Recursive Program
- int puzzle(int N)
-
- if (N 1)
- return 1
- if (N 2 0)
- return puzzle(N/2)
- else
- return puzzle(3N1)
-
Whats Makes this Program Questionable?
This recursive call is not smaller than the
original. Cannot prove it terminates. But it
does.
Sedgewick Program 5.2
9Sample Recursive Program Euclids Greatest
Common Divisor
- int gcd(int m, int n)
-
- if (n 0)
- return m
- return gcd(n, m n)
-
Sedgewick Program 5.3
10Sample Recursive Program Linked List
- int count(link x)
-
- if (x NULL)
- return 0
- return 1 count(x-gtnext)
-
- void traverse(link h, void (visit)(link))
-
- if (h NULL)
- return
- (visit)(h)
- traverse(h-gtnext, visit)
Sedgewick Program 5.5
11Sample Recursive Program Linked List (cont)
- void traverseR(link h, void (visit)(link))
-
- if (h NULL) return
- traverseR(h-gtnext, visit)
- (visit)(h)
-
- link delete(link x, Item v)
- / Delete a nodes, and returns remainder of
list / - if (x NULL) return NULL
- if (eq(x-gtitem, v))
- link t x-gtnext free(x) return t
- x-gtnext delete(x-gtnext, v)
- return x
-
Sedgewick Program 5.5
12Building Bridges Looping
- Recursion and looping follow similar thought
processes. - A loops termination condition serves the same
role as a recursive base case. - A loops control variable serves the same role as
a general case.
sum 0 i 1 while (i lt 10) sum
i i
int iFactorial(int n) if (n 0) // base
case return 1 else // n
gt 0, recursive case return
niFactorial(n-1)
Termination Condition
Loop control and recursive case both move toward
termination condition
What happens if loop control and recursive case
does not move toward termination condition?
13Realistic Example Directory Searching
- A more common example of recursion is searching
through a hard disk directory structure. - Analysis
- Directories are made up of files and
subdirectories. - Process the files within a directory, and then
process each if its subdirectories recursively. - Count the number of files that match the search
pattern. - What is the base case?
- What is the recursive case?
- What is the maximum depth of recursion?
- How many .tmp files do you think are on your C
drive?
14Implementation of Directory Search
- using System
- using System.IO
- namespace RecursiveDirectoryFileSearch
-
- class RecursiveDirectoryFileSearch
-
- static void main(string args)
-
- Console.WriteLine("Found 0
occurrences.", - iDirectorySearch(_at_"C\",".tmp"))
- Console.WriteLine(
- "Press enter to continue")
- Console.ReadLine()
-
-
This implementation is in C.
15Implementation of Directory Search (cont)
- // ltreturnsgt Number of files found that
match the patternlt/returnsgt - internal static int iDirectorySearch(
- string asDirectory,
- string asSearchPattern)
-
- // Local variables
- int liFileCounter 0
- // Process each file
- foreach (string lsFileName in
- Directory.GetFiles(asDirectory,
asSearchPattern)) -
- Console.WriteLine(lsFileName)
- liFileCounter
-
- // Process each subdirectory
- foreach (string lsSubdirectory in
- Directory.GetDirectories(asDirecto
ry))
Base Case
Recursive Case
16Acknowledgements
- Robert Sedgewick. Algorithms in C.