Data structures csci 132 spring 2014 lecture 16 tail recursion
This presentation is the property of its rightful owner.
Sponsored Links
1 / 33

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


  • 89 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

Data Structures CSCI 132, Spring 2014 Lecture 16 Tail Recursion

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


Data structures csci 132 spring 2014 lecture 16 tail recursion

Data StructuresCSCI 132, Spring 2014Lecture 16Tail Recursion


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

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


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;

}


Recall fibonacci

Recall Fibonacci


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

}

}


Recursive fibonacci repeats computations

Recursive Fibonacci repeats computations


Fibonacci as a tail recursion

Fibonacci as a Tail Recursion

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


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.


Tower of hanoi demo

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo1

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo2

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo3

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo4

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo5

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo6

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo7

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo8

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo9

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo10

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo11

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo12

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo13

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo14

Tower of Hanoi Demo

B

C

A


Tower of hanoi demo15

Tower of Hanoi Demo

B

C

A


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?


  • Login