data structures csci 132 spring 2014 lecture 16 tail recursion n.
Download
Skip this Video
Download Presentation
Data Structures CSCI 132, Spring 2014 Lecture 16 Tail Recursion

Loading in 2 Seconds...

play fullscreen
1 / 33

Data Structures CSCI 132, Spring 2014 Lecture 16 Tail Recursion - PowerPoint PPT Presentation


  • 108 Views
  • Uploaded on

Data Structures CSCI 132, Spring 2014 Lecture 16 Tail Recursion. Recall Recursive Skating. void skateLaps( int n) { if ( n < = 0 ) { //do nothing } else { cout << "Skating 1 lap." << endl; skateLaps( n - 1); } }. Base Case. General Case. Tail Recursion.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Data Structures CSCI 132, Spring 2014 Lecture 16 Tail Recursion' - sauda


Download Now 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
recall recursive skating
Recall Recursive Skating

void skateLaps( int n) {

if ( n < = 0 ) {

//do nothing

} else {

cout << "Skating 1 lap." << endl;

skateLaps( n - 1);

}

}

Base Case

General Case

tail recursion
Tail Recursion
  • When the recursive function call is the last thing that occurs in the recursive function, this is called a Tail Recursion.
  • In a tail recursion, there are no pending operations.
  • Any tail recursion can be written as an iteration (a loop).
iterating skatelaps
Iterating skateLaps

void skateLaps( int n) {

while (n > 0 ) {

cout << "Skating 1 lap." << endl;

n = n - 1;

}

}

factorial revisited
Factorial Revisited

int Fact (int n) {

if (n <= 0) {

return 1;

} else {

return n * Fact (n - 1);

}

}

Is this a tail recursion?

No.

Can we write this as a tail recursion?

Yes!

Keep track of the answer in a parameter that gets passed on.

tail recursion factorial
Tail Recursion Factorial

int factIter (int n) { //Helper function to initialize ans

return factTail(n, 1);

}

int factTail (int num, int ans) {

if (num <= 0) {

return ans;

} else {

return factTail(num - 1, num*ans);

}

}

execution of facttail
Execution of FactTail

num ans num*ans Function Calls

4 1 4 Fact(4, 1):24

3 4 12 Fact(3, 4):24

2 12 24 Fact(2, 12):24

1 24 24 Fact(1, 24):24

0 24 (returned) Fact(0, 24):24

writing facttail as an iteration
Writing factTail as an iteration

int factWhile (int n) {

}

writing facttail as an iteration1
Writing factTail as an iteration

int factWhile (int n) {

int num = n;

int ans = 1;

while (num > 0) {

ans = ans * num;

num = num - 1;

}

return ans;

}

fibonacci in c code
Fibonacci in C++ code

int Fib( int n ) {

if (n < 2) {

return n;

} else {

return Fib(n-1) + Fib(n -2);

}

}

fibonacci as a tail recursion
Fibonacci as a Tail Recursion

(fib(i) + fib(i-1))

n i fib(i-1) fib(i) fib(i_next)

5 1 0 1 1

5 2 1 1 2

5 3 1 2 3

5 4 2 3 5

5 5 3 5 8

Pass values as parameters of the recursive function:

n, i, fibIminus1, fibI (we will work on this in lab).

the tower of hanoi
The Tower of Hanoi

Setup: A stack of rings in order of largest to smallest is on one of three pegs (largest on the bottom, smallest on the top).

Object: Move stack from one peg to another peg following these rules:

1) Cannot put a ring on top of a smaller ring.

2) Can only move 1 ring at a time.

strategy for tower of hanoi
Strategy for Tower of Hanoi

1) Move top (n-1) rings from origin to spare

(following all the rules).

2) Move bottom ring from origin to destination

3) Move top (n-1) rings from spare to destination

(following all the rules).

implementing tower of hanoi
Implementing Tower of Hanoi

void hanoi(int n, char start, char end, char spare) {

if (n < = 0) {

//do nothing

} else {

hanoi(n-1, start, spare, end);

cout << "Move disk " << n << "from "

<< start << " to " << end << endl;

hanoi(n-1, spare, end, start);

}

}

//Outputs sequence of moves to solve tower of hanoi!

invocation tree for hanoi
Invocation Tree for hanoi

nodes

20

21

22

23

24

How long will it take to move a tower 100 rings high?

ad