Docsity

Review of Recursion - Advanced Programming - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Review of Recursion - Advanced Programming - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Review of Recursion

For those of you that have slept

since then…

Why 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!

Simple 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

n! = n* (n-1)!

where n is not negative.

Base Case

Recursive Case

Implementation

int factorial(int N)

if (N == 0)

return 1;

return N*factorial(N-1);

Base Case

Recursive Case

Sedgewick Program 5.

A 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.)

Questionable Recursive Program

int puzzle(int N)

if (N == 1)

return 1;

if (N % 2 == 0)

return puzzle(N/2);

else

return puzzle(3*N+1);

What’s Makes this

Program Questionable?

This recursive call is not

smaller than the original.

Cannot prove it terminates.

But it does….

Sedgewick Program 5.

Sample Recursive Program: Linked List

int count(link x) { if (x == NULL) return 0; return 1 + count(x->next); } void traverse(link h, void (visit)(link)) { if (h == NULL) return; (visit)(h); traverse(h->next, visit); } Sedgewick Program 5.

Sample Recursive Program: Linked List (cont)

void traverseR(link h, void (visit)(link)) { if (h == NULL) return; traverseR(h->next, 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->item, v)) { link t = x->next; free(x); return t; } x->next = delete(x->next, v); return x; } Sedgewick Program 5.

Realistic 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?

usingImplementation of Directory Search System; using System.IO; namespace RecursiveDirectoryFileSearch { class RecursiveDirectoryFileSearch { static void main(string[] args) { Console.WriteLine("Found {0} occurrences.", iDirectorySearch(@"C:","*.tmp")); Console.WriteLine( "Press enter to continue"); Console.ReadLine(); } } } This implementation is in C#.