1 / 33

Data Structures CSCI 132, Spring 2014 Lecture 16 Tail Recursion

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.

sauda
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data StructuresCSCI 132, Spring 2014Lecture 16Tail Recursion

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

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

  4. Iterating skateLaps void skateLaps( int n) { while (n > 0 ) { cout << "Skating 1 lap." << endl; n = n - 1; } }

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

  6. 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); } }

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

  8. Writing factTail as an iteration int factWhile (int n) { }

  9. 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; }

  10. Recall Fibonacci

  11. Fibonacci in C++ code int Fib( int n ) { if (n < 2) { return n; } else { return Fib(n-1) + Fib(n -2); } }

  12. Recursive Fibonacci repeats computations

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

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

  15. Tower of Hanoi Demo B C A

  16. Tower of Hanoi Demo B C A

  17. Tower of Hanoi Demo B C A

  18. Tower of Hanoi Demo B C A

  19. Tower of Hanoi Demo B C A

  20. Tower of Hanoi Demo B C A

  21. Tower of Hanoi Demo B C A

  22. Tower of Hanoi Demo B C A

  23. Tower of Hanoi Demo B C A

  24. Tower of Hanoi Demo B C A

  25. Tower of Hanoi Demo B C A

  26. Tower of Hanoi Demo B C A

  27. Tower of Hanoi Demo B C A

  28. Tower of Hanoi Demo B C A

  29. Tower of Hanoi Demo B C A

  30. Tower of Hanoi Demo B C A

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

  32. 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!

  33. Invocation Tree for hanoi nodes 20 21 22 23 24 How long will it take to move a tower 100 rings high?

More Related