1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
Advertisements

CSC 205 Programming II Lecture 10 Towers of Hanoi.
COSO 1030 Section 4 Recursion. What is about Towers of Hanoi Divide and Conquer Strategy Recursion and Induction Thinking Recursively Recursion Pitfalls.
COSC 2006 Data Structures I Recursion III
Factorial Recursion stack Binary Search Towers of Hanoi
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Recursion Gordon College CPS212
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Recursion Jordi Cortadella Department of Computer Science.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Data Structures Recursion Phil Tayco Slide version 1.0 Mar. 8, 2015.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CS111 Computer Programming Department of Computer Science Wellesley College Classic Recursion Examples Plus Class Methods and Applications Tuesday, October.
Recursion. 2 Overview  Learn about recursive definitions  Explore the base case and the general case of a recursive definition  Discover recursive.
Recursion. Circular Definition (not useful) E.g., E.g., Projenitor: one who produces an offspring Projenitor: one who produces an offspring Offspring:
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
15. MORE RECURSIONS Rocky K. C. Chang November 22, 2015.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
UNIT 17 Recursion: Towers of Hanoi.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion.
Tower of Hanoi problem: Move the pile of rings from one peg to another
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
CprE 185: Intro to Problem Solving (using C)
Abdulmotaleb El Saddik University of Ottawa
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Chapter 10.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
Recursion Chapter 11.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Data Structures.
Tower of Hanoi problem: Move the pile of rings from one peg to another
The Hanoi Tower Problem
Recursion When performing a repetitive task either: a loop recursion
Chapter 17 Recursion.
Chapter 19: Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Tower of Hanoi problem: Move the pile of rings from one peg to another
Presentation transcript:

1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion

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

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

4 Tail Recursion Factorial int factTail (int num, int ans) { if (num <= 0) { return ans; } else { return factTail(num - 1, num*ans); } } int factIter (int n) {//Helper function to initialize ans return factTail(n, 1); }

5 Execution of FactTail numansnum*ansFunction Calls 414Fact(4, 1): Fact(3, 4): Fact(2, 12): Fact(1, 24): (returned)Fact(0, 24):24

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

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

8 Recall Fibonacci

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

10 Recursive Fibonacci repeats computations

11 Fibonacci as a Tail Recursion nifib(i-1)fib(i)fib(i_next) (fib(i) + fib(i-1)) Pass values as parameters of the recursive function: n, i, fibIminus1, fibI (we will work on this in lab).

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

13 Tower of Hanoi Demo A BC

14 Tower of Hanoi Demo A BC

15 Tower of Hanoi Demo A BC

16 Tower of Hanoi Demo A BC

17 Tower of Hanoi Demo A BC

18 Tower of Hanoi Demo A BC

19 Tower of Hanoi Demo A BC

20 Tower of Hanoi Demo A BC

21 Tower of Hanoi Demo A BC

22 Tower of Hanoi Demo A BC

23 Tower of Hanoi Demo A BC

24 Tower of Hanoi Demo A BC

25 Tower of Hanoi Demo A BC

26 Tower of Hanoi Demo A BC

27 Tower of Hanoi Demo A BC

28 Tower of Hanoi Demo A BC

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

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

31 Invocation Tree for hanoi nodes How long will it take to move a tower 100 rings high?