Tail and Non-tail Recursion

Slides:



Advertisements
Similar presentations
Recursion Recursive Thinking Recursive Programming
Advertisements

Types of Recursive Methods
Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Recursion Outline of the upcoming lectures: Review of Recursion
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
More on Recursive Methods KFUPM- ICS Data Structures.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
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 Outline of the upcoming lectures: –Review of Recursion –Types of Recursive Methods –Final Remarks on Recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion!. Can a method call another method? YES.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 8: Recursion Data Structures.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Some Notes on Recursion Data.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
Functions in C. Consider #include main() { int i; for(i=1; i
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
ASET RECURSION. ASET RECURSIVE FUNCTIONS A recursive function is a function that calls itself to solve a smaller version of its task until a final call.
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
Recursion. Circular Definition Circular definition Circular definition Dialectic materialism is materialism that is dialectic. Dialectic materialism is.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Functions-Recall 1. 2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2:
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
CSE 3358 NOTE SET 8 Data Structures and Algorithms.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
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.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Tail and Non-tail Recursion Tail Recursion? –Definition –Advantages of tail recursion –Converting to and from tail recursion Indirect Recursion? –Definition.
1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Topic 6 Recursion.
RECURSION.
Data Structures and Algorithms
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
CSE 341 Lecture 5 efficiency issues; tail recursion; print
More on Recursive Methods
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Lesson #6 Modular Programming and Functions.
Applied Algorithms (Lecture 17) Recursion Fall-23
Stacks & Recursion.
Recursion.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Recursion.
Unit 3 Test: Friday.
Module 1-10: Recursion.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
2/24/2019.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Lesson #6 Modular Programming and Functions.
MIPS function continued
Yan Shi CS/SE 2630 Lecture Notes
CS1100 Computational Engineering
Chapter 5 Recursion.
Types of Recursive Methods
Presentation transcript:

Tail and Non-tail Recursion Definition Advantages of tail recursion Converting to and from tail recursion Indirect Recursion? Examples

What is Tail Recursion? Recursive methods are either Tail recursive Nontail recursive Tail recursive method has the recursive call as the last statement in the method. Recursive methods that are not tail recursive are called non-tail recursive

Is Factorial Tail Recursive? Is the factorial method a tail recursive method? int fact(int x){ if (x==0) return 1; else return x*fact(x-1); } When returning back from a recursive call, there is still one pending operation, multiplication. Therefore, factorial is a non-tail recursive method.

Another Example Is this method tail recursive? It is tail recursive! void tail(int i) { if (i>0) { system.out.print(i+"") tail(i-1) } It is tail recursive!

Third Example Is the following program tail recursive? void non prog(int i) { if (i>0) { prog(i-1); System.out.print(i+""); } No, because there is an ealier recursive call, other than the last one, In tail recursion, the recursive call should be the last statement, and there should be no earlier recursive calls whether direct or indirect.

Advantage of Tail Recursive Method Tail Recursive methods are easy to convert to iterative. Smart compilers can detect tail recursion and convert it to iterative to optimize code Used to implement loops in languages that do not support loop structures explicilty (e.g. prolog) void tail(int i){ if (i>0) { system.out.println(i+""); tail(i-1) } void iterative(int i){ for (;i>0;i--) System.out.println(i+""); }

Converting Non-tail to Tail Recursive A non-tail recursive method can be converted to a tail-recursive method by means of an "auxiliary" parameter used to form the result. The technique is usually used in conjunction with an "auxiliary" function. This is simply to keep the syntax clean and to hide the fact that auxiliary parameters are needed. int fact_aux(int n, int result) { if (n == 1) return result; return fact_aux(n - 1, n * result) } int fact(n) { return fact_aux(n, 1);

Converting Non-tail to Tail Recursive A tail-recursive Fibonacci method can be implemented by using two auxiliary parameters for accumulating results. int fib_aux ( int n , int next, int result) { if (n == 0) return result; return fib_aux(n - 1, next + result, next); } To calculate fib(n) , call fib_aux(n,1,0) auxiliary parameters!

Converting Tail Recursive to Iterative F(x) { if (P(x)) return G(x); return F(H(x)); } P(x) is true, the value of F(x) is the value of some other function G(x). Otherwise, the value of F(x) is the value of the function F on some other value, H(x) int temp_x = x; while (P(x) is not true) { temp_x = x; x = H(temp_x);

Converting Tail Recursive to Iterative int fact_aux(int n, int result) { if (n == 1) return result; return fact_aux(n - 1, n * result); } F(x) { if (P(x)) return G(x); return F(H(x)); } the function F is fact_aux x is composed of the two parameters, n and result the value of P(n, result) is the value of (n == 1) the value of G(n, result) is result the value of H(n, result) is (n -1, n * result) int fact_iter(int n, int result) { int temp_n; int temp_result; while (n != 1) { temp_n = n; temp_result = result; n = temp_n - 1; result = temp_n * temp_result; } return result; F(x) { int temp_x = x; while (P(x) is not true) temp_x = x; x = H(temp_x); } return G(x);

Indirect Recursion If X makes a recursive call to X itself, it is called direct recursion. Indirect recursive methods call themselves indirectly through calling other methods. In general, indirect recursion is a circular sequence of two or more recursive calls: X1 --> X2 --> ... --> X1.

Indirect Recursion Example Three method for decoding information are: receive: stores information in buffer and calls decode. decode: converts information to new form and calls store store : stored information in a file, call receive. We have a chain of calls: recieve() -> decode() -> store() ->recieve()-> decode()....

Example: Information Decoding recieve (buffer) while buffer is not filled up if information is still incoming get a character and store it in buffer; else exit() decode (buffer); decode(buffer) decode information in buffer; store (buffer); store(buffer) transfer information from buffer to file; recieve (buffer);

Indirect Recursion:Another Example Determine whether an integer is even or odd: How do we know if a number is even? we know 0 is even. we know that if n is even, then n-1 must be odd. static boolean is_even(int n) { if (n==0) return true; else return(is_odd(n-1)); } How do we know if a number is odd? It's not even! tatic boolean is_odd(int n) return (!is_even(n));