- 138 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' Recursion' - kelly-shelton

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Recursive Functions

- Recursive programming is based off of Recursive Formulas
- Definition of Recursive Formula
- a formula that is used to determine the next term of a sequence using one or more of the preceding terms.

- Example of Recursive Formula
- The recursive formula for the sequence 5, 20, 80, 320, ... is an = 4 an-1
How would you program such a thing?

- The recursive formula for the sequence 5, 20, 80, 320, ... is an = 4 an-1

Program Solution

int series(int n)

{

if (n == 1) return 5;

return 4 * series(n-1);

}

Base case

Recursive case

Recursion

A function is defined recursively if it has the following two parts:

- An anchor or base case
- The function is defined for one or more specific values of the parameter(s)

- An inductive or recursive case
- The function's value for current parameter(s) is defined in terms of previously defined function values and/or parameter(s)

Recursive Example

- Consider a recursive power functiondouble power (double x, unsigned n){ if ( n == 0 ) return 1.0; else return x * power (x, n-1); }
- Which is the anchor?
- Which is the inductive or recursive part?
- How does the anchor keep it from going forever?

Recursive Example

- Note the results of a call
- Recursivecalls
- Resolutionof thecalls

Recursive Factorialanother example

- n! = 1 x 2 x …x n, for n > 0
n! = (n – 1)! X n

5! = 5 x 4! 120

4! = 4 x 3! 24

3! = 3 x 2! 6

2! = 2 x 1! 2

1! = 1 1

Another Example of Recursion

- Fibonacci numbers1, 1, 2, 3, 5, 8, 13, 21, 34f1 = 1, f2 = 1 … fn = fn -2 + fn -1
- A recursive functiondouble Fib (unsigned n){ if (n <= 2) return 1; else return Fib (n – 1) + Fib (n – 2); }

A Bad Use of Recursion

- Why is this inefficient?
- Note the recursion tree

Uses of Recursion

- Binary Search
- See source code
- Note results of recursive call

Uses of Recursion

- Palindrome checker
- A palindrome has same value with characters reversed1234321 racecar
Recursive algorithm for an integer palindrome checker

If numDigits <= 1 return true

Else check first and last digits num/10numDigits-1and num % 10

- if they do not match return false
If they match, check more digitsApply algorithm recursively to:(num % 10numDigits-1)/10 and numDigits - 2

- if they do not match return false

- A palindrome has same value with characters reversed1234321 racecar

Recursion Example: Towers of Hanoi

- Think Recursive algorithm
- Task
- Move disks from left peg to right peg
- When disk moved, must be placed on a peg
- Only one disk (top disk on a peg) moved at a time
- Larger disk may never be placed on a smaller disk

Recursion Example: Towers of Hanoi

- Identify base case:If there is one disk move from A to C
- Inductive solution for n > 1 disks
- Move topmost n – 1 disks from A to B, using C for temporary storage
- Move final disk remaining on A to C
- Move the n – 1 disk from B to C using A for temporary storage

Towers of Hanoi: Solution

voidhanoi(int n, conststring& initNeedle,

conststring& endNeedle, conststring& tempNeedle)

{

if (n == 1)

cout << "move " << initNeedle << " to "

<< endNeedle << endl;

else

{

hanoi(n-1,initNeedle,tempNeedle,endNeedle);

cout << "move " << initNeedle << " to "

<< endNeedle << endl;

hanoi(n-1,tempNeedle,endNeedle,initNeedle);

}

}

Towers of Hanoi: Solution

string beginneedle = "A", middleneedle = "B", endneedle = "C";

hanoi(3, beginneedle, endneedle, middleneedle);

The solution for n = 3

move A to C

move A to B

move C to B

move A to C

move B to A

move B to C

move A to C

output

Recursion Example: Towers of Hanoi

- Note the graphical steps to the solution

Recursion Example: Parsing

- Examples so far are direct recursion
- Function calls itself directly

- Indirect recursion occurs when
- A function calls other functions
- Some chain of function calls eventually results in a call to original function again

- An example of this is the problem of processing arithmetic expressions

Recursion Example: Parsing

- Parser is part of the compiler
- Input to a compiler is characters
- Broken up into meaningful groups
- Identifiers, reserved words, constants, operators

- These units are called tokens
- Recognized by lexical analyzer
- Syntax rules applied

Recursion Example: Parsing

- Parser generates a parse tree using the tokens according torules below:
- An expression:term + term | term – term | term
- A term:factor * factor | factor / factor | factor
- A factor:( expression ) | letter | digit

Note the indirect recursion

Implementing Recursion:The Runtime Stack

- Activation record created for each function call
- Activation records placed on run-time stack
- Recursive calls generate stack of similar activation records

Implementing Recursion:The Runtime Stack

- When base case reached and successive calls resolved
- Activation records are popped off the stack

Download Presentation

Connecting to Server..