Loading in 5 sec....

Data Structures CSCI 132, Spring 2014 Lecture 16 Tail RecursionPowerPoint Presentation

Data Structures CSCI 132, Spring 2014 Lecture 16 Tail Recursion

Download Presentation

Data Structures CSCI 132, Spring 2014 Lecture 16 Tail Recursion

Loading in 2 Seconds...

- 98 Views
- Uploaded on
- Presentation posted in: General

Data Structures CSCI 132, Spring 2014 Lecture 16 Tail Recursion

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

void skateLaps( int n) {

if ( n < = 0 ) {

//do nothing

} else {

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

skateLaps( n - 1);

}

}

Base Case

General Case

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

void skateLaps( int n) {

while (n > 0 ) {

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

n = n - 1;

}

}

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.

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);

}

}

numansnum*ansFunction Calls

414Fact(4, 1):24

3412Fact(3, 4):24

21224Fact(2, 12):24

12424Fact(1, 24):24

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

int factWhile (int n) {

}

int factWhile (int n) {

int num = n;

int ans = 1;

while (num > 0) {

ans = ans * num;

num = num - 1;

}

return ans;

}

int Fib( int n ) {

if (n < 2) {

return n;

} else {

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

}

}

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

nifib(i-1)fib(i)fib(i_next)

51011

52112

53123

54235

55358

Pass values as parameters of the recursive function:

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

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.

B

C

A

B

C

A

B

C

A

B

C

A

B

C

A

B

C

A

B

C

A

B

C

A

B

C

A

B

C

A

B

C

A

B

C

A

B

C

A

B

C

A

B

C

A

B

C

A

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

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!

nodes

20

21

22

23

24

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