Type of Recursion
Type of Recursion
2019390004
Write all type of recursive methods (about 20 methods), describe each of them using your own
word, and give a brief code example.
1. Linear Recursive
A linear recursive function is one that only makes one call to itself each time the function
is run (as opposed to a function that calls itself multiple times during its execution).
Factorial functions are good examples of linear recursion.
Code:
import java.util.Scanner;
return result;
}
}
Result
2. Tail recursive
Tail recursion is a form of linear recursion. In tail recursion, the recursive call is the last
thing the function does. Often, recursive call values are returned. A great example of a
tail-recursive function is a function for calculating the GCD, or Largest Common
Denominator, from two numbers:
int gcd (int m, int n) {int r; if (m <n) return gcd (n, m); r = m% n; if (r == 0) returns (n);
else return (gcd (n, r));}
Code:
import java.util.Scanner;
public class GCDExample1 {
}
Result
3. Binary Recursive
Some recursive functions do not just have one call, they have two (or more). A function
with two recursive calls is called a binary recursive function. Mathematical combination
operations are a great example of a function that can be quickly implemented as a
recursive binary function. The number of combinations, often represented as nCk where
we select n elements from a set of k elements, can be implemented as follows:
int select (int n, int k) {if (k == 0 || n == k) return (1); else return (select (n-1, k) + select
(n-1, k-1));}
Code:
class BinarySearch {
int mid = l + (r - l) / 2;
4. Exponential Recursion
An exponential recursive function is a function that, if you draw a representation of all
the function calls, will have an exponential number of calls about the size of the data set
(meaning exponential if there are n elements, there will be O (a) function calls where a is
a positive number). A great example of an exponentially recursive function is a function
for calculating all permutations of a data set. Let us write a function to take an array of n
integers and print each one.
clear print_array (int arr [], int n) {int i; for (i = 0; i <n; i) printf ("% d", arr [i]); printf
("\ n"); } void print_permutations (int arr [], int n, int i) {int j, swap; print_array (arr,
n); for (j = i + 1; j <n; j) {swap = arr [i]; arr [i] = arr [j]; arr [j] = exchange;
print_permutations (arr, n, i + 1); exchange = arr [i]; arr [i] = arr [j]; arr [j] =
exchange; }}
To run this function on an array of arrays of length n, we would do print_permutations
(arr, n, 0) where 0 tells to start over from the beginning of the array
Code:
import java.text.*;
class Exponential
{
// Termination condition
if (n == 0)
return 1;
// Recursive call
r = e(x, n - 1);
// Factorial
f = f * n;
return (r + p / f);
}
// Driver code
public static void main (String[] args)
{
int x = 4, n = 15;
DecimalFormat df = new DecimalFormat("0.######");
System.out.println(df.format(e(x, n)));
}
}
Result:
5. Nested Recursion
In nested recursion, one of the arguments for a recursive function is the recursive
function itself! This function tends to develop very quickly. A good example is the
classical math function, "Ackerman's function. It grows very fast (even for small values
of x and y, Ackermann (x, y) is very large) and cannot be computed by just iteration
definite (for () loops for example); it requires infinite iteration (recursion, for example).
Ackerman function int ackerman (int m, int n) {if (m == 0) return (n + 1); else if (n ==
0) return (ackerman (m-1,1)); else return (ackerman (m-1, ackerman (m, n-1))); }
Code:
public class Nested2 {
// Assumes n >= 0, m >= 0
public static int ack(int m, int n) {
if (m == 0) {
return n + 1;
}
else if (n == 0) {
return ack(m - 1, 1);
}
else {
return ack(m - 1, ack(m, n - 1));
}
}
tree
6. Mutual Recursion
Recursive functions do not need to call themselves. Some recursive functions work in
pairs or even in larger groups. For example, function A calls function B which calls
function C which in turn calls function A. A simple example of reciprocal recursion is a
series of functions for determining whether an integer is even or odd. How do we know if
a number is even? Well, we know 0 is even. And we also know that if a number n is
even, then n - 1 must be odd. How do we know if a number is odd? It is not even!
int is even (unsigned int n) {if (n == 0) return 1; else return (is odd (n-1)); } int is odd
(unsigned int n) {return (! is even (n)); }
The above situation is not the best example when we want to use recursion instead of
iteration or closed form solutions. A more efficient set of functions for determining
whether an integer is even or odd is as follows:
int is even (unsigned int n) {if (n% 2 == 0) return 1; else returns 0;} int is odd (unsigned
int n) {if (n% 2! = 0) return 1; else returns 0;}
Code:
import java .io.*;
class Mutual {
// Female function
static int hofstaderFemale(int n)
{
if (n < 0)
return 0;
else
return (n == 0) ? 1 : n -
hofstaderFemale(n - 1);
}
// Male function
static int hofstaderMale(int n)
{
if (n < 0)
return 0;
else
return (n == 0) ? 0 : n -
hofstaderMale(n - 1);
}
// Driver Code
static public void main (String[] args)
{
int i;
System.out.print("F: ");
for (i = 0; i < 20; i++)
System.out.print(hofstaderFemale(i)
+ " ");
System.out.println();
System.out.print("M: ");
for (i = 0; i < 20; i++)
System.out.print(hofstaderMale(i)
+ " ");
}
}
Result:
7. Head Recursion
If the recursive function calls itself and the recursive call is the first statement in the
function, then this is known as Head Recursion. No statement, no pre-call operation. The
function does not have to process or perform any operation at the time of calling and all
operations are performed at the time of return.
Code:
public class Head
{
public static void mistery(int x)
{
if(x>0)
mistery(x-1);
System.out.println(x);
}
8. Tree Recursion
Tree Recursion: To understand Tree Recursion, let's first understand Linear Recursion. If
a function is recursive calling itself for a time, then it is known as Linear Recursion.
Conversely, if a recursive function calls itself more than once then it is called Tree
Recursion.
Code:
class Node
{
int data;
Node left, right;
9. Indirect Recursion
In this recursion, there may be more than one function and they call each other in a circle.
Code:
class Indirect
{
// We can avoid use of these using references
static final int N = 20;
static int n = 1;
// Driver Program
public static void main(String[] args)
{
fun1();
}
}
Result:
stdIn.close();
}
}
Result:
stdIn.close();
}
Result:
int main()
{
long x;
for (x = -1; x < 4; x ++)
printf("fib %ld = %ld\n", x, fib(x));
return 0;
}
Result:
13. Structural Recursion
The term "structural recursion" comes from the fact that this structure can be recursively
defined:
A list does not mean anything, or the cells followed by a list.
A binary tree means nothing or a node with two binary trees as children.
When performing structural recursion, you "undo" the operation from which these
structures are built on each other. For example, the NumberOfNodes function "undo"
construction takes a node and adds it to an existing list. The Find operator "undoes" the
binding operation of the node to the other two trees. Therefore, it's easy to see why this
function should be terminated - in the end, you "undo" all operations that were performed
to build the object in the first place, and the recursion stop
Code:
import java.util.Scanner;
Code:
import java.util.Scanner;
num1 = (int)scanner.nextInt();
num2 = (int)scanner.nextInt();
scanner.close();
else
System.out.println ("");
Result
int fun(int n)
{
if (n == 4)
return n;
else return 2*fun(n+1);
}
int main()
{
printf("%d ", fun(2));
return 0;
}
Result: