0% found this document useful (0 votes)
296 views

Tail and Non-Tail Recursion

The document discusses tail and non-tail recursion, defining tail recursion as having the recursive call as the last statement and providing advantages like being easily converted to iteration, while non-tail recursion has operations after the recursive call. Examples are provided to illustrate tail and non-tail recursion, and techniques for converting between the two forms like using auxiliary parameters and functions. Indirect recursion, where methods recursively call each other in a cycle, is also covered with examples.

Uploaded by

Vandan Thaker
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
296 views

Tail and Non-Tail Recursion

The document discusses tail and non-tail recursion, defining tail recursion as having the recursive call as the last statement and providing advantages like being easily converted to iteration, while non-tail recursion has operations after the recursive call. Examples are provided to illustrate tail and non-tail recursion, and techniques for converting between the two forms like using auxiliary parameters and functions. Indirect recursion, where methods recursively call each other in a cycle, is also covered with examples.

Uploaded by

Vandan Thaker
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Tail and Non-tail Recursion

• Tail Recursion?
– Definition
– Advantages of tail recursion
– Converting to and from tail recursion

• Indirect Recursion?
– Definition
– 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?

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+"");
prog(i-1);
}
}

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

void tail(int i){ void iterative(int i){


if (i>0) { for (;i>0;i--)
system.out.println(i+""); System.out.println(i+"");
tail(i-1) }
}
}

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

auxiliary parameters!
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)


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)
F(x) {
int temp_x = x;
while (P(x) is not true) {
temp_x = x;
x = H(temp_x);
}
return G(x);
}
Converting Tail Recursive to Iterative
int fact_aux(int n, int result) { F(x) {
if (n == 1) return result; if (P(x)) return G(x);
return fact_aux(n - 1, n * result); 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) { F(x)


int temp_n; {
int temp_result; int temp_x = x;
while (n != 1) { while (P(x) is not true)
temp_n = n; {
temp_result = result; temp_x = x;
n = temp_n - 1; x = H(temp_x);
result = temp_n * temp_result; }
} return G(x);
return result; }
}
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));
}

You might also like