Javatpoint Logo
Javatpoint Logo

Different Types of Recursions in Java

A process in which a function or method is calling itself is known as recursion. Recursion is one of the prominent topics in Java. In this tutorial, we are going to discuss the different types of recursions in Java.

Recursion Types

There are mainly two types of recursions:

1) Direct Recursion

Direct recursion means the method is calling itself directly. Direct recursion can be further categorized into five parts:

a) Tail Recursion: In the tail recursion, the function calls itself, and the statement that is responsible for the recursion is the last statement of the method. In other words, after the recursive statement, no statement should be written after it. If the recursive function is invoking itself and that recursive call is the last statement in the function, then it's known as Tail Recursion. The function has to perform or process any operation at the time of calling, and it does nothing at the return time.

FileName: TailRecursion.java

Output:

The value is 4
The value is 3
The value is 2
The value is 1

Complexity Analysis: The time complexity of the program is O(n). It is because the statement that is responsible for the recursion runs n times. The space complexity of the program is O(n).

Let's see what happens if we do the same thing using a loop.

FileName: TailRecursion1.java

Output:

The value is 4
The value is 3
The value is 2
The value is 1

Complexity Analysis: The time complexity of the program is O(n). It is because the loop runs n times. The space complexity of the program is O(1).

It is evident by looking at these two programs that the performance of the iteration (using loops) is better than recursion as the iteration takes less space. Let's explore the reason for it.

In the case of a loop, when the function "(void functn(int y))" executes, there is a creation of only one activation record maintained in the stack (activation record for the variable 'y'). Therefore, only one unit of memory inside the stack is created, making the space complexity of the program O(1). In contrast, in the case of recursion, a separate activation record is created each time the function calls itself, making the space complexity of the program O(n), provided the recursion lasts n times.

b) Head Recursion: In the head recursion, the statement that is responsible for the recursion is the first statement of the function. In other words, no statement regarding operation should occur before the statement that is responsible for the recursion.

FileName: HeadRecursion.java

Output:

The value is 1
The value is 2
The value is 3
The value is 4

Complexity Analysis: The time complexity of the program is O(n), and the space complexity of the program is also O(n).

c) Linear Recursion: When a function is calling itself, but only one time, then it is called linear recursion. Observe its pseudo code.

The programs that have been discussed in the head and tail recursion are also examples of linear recursion.

d) Tree Recursion: When a function is calling itself, but more than one time, then it is called tree recursion. Observe the following example.

FileName: TreeRecursion.java

Output:

The value is 4
The value is 3
The value is 2
The value is 1
The value is 1
The value is 2
The value is 1
The value is 1
The value is 3
The value is 2
The value is 1
The value is 1
The value is 2
The value is 1
The value is 1

Complexity Analysis: The time complexity of the program is O(2n), and the space complexity of the program is also O(2n). The time and space complexity is exponential because one recursive call leads to two separate recursive calls.

e) Nested Recursion: In this recursion, a function will call itself, and the statement that is responsible for recursion is nested, meaning there is a recursive call inside the parameter of the recursive call. In other words, "inside recursion, there is recursion". The following example will make the concept clearer.

FileName: NestedRecursion.java

Output:

The value is 4
The value is 3
The value is 2
The value is 1

Complexity Analysis: The time complexity of the program is O(n), and the space complexity of the program is also O(n).

2) Indirect Recursion

Suppose there is a function called funA(), and this function is calling another function called funB(), and this function is calling another function called funC(), and funC() function is calling the function funA(), then this type of recursion is called indirect recursion. We see that there is a chain function funA() invokes function funB(), function funB() invokes function funC(), and function funC() invokes function funA(). Let us see its implementation.

FileName: IndirectRecursion.java

Output:

In function A, the value is 10
In function B, the value is 9
In function C, the value is 8
In function A, the value is 7
In function B, the value is 6
In function C, the value is 5
In function A, the value is 4
In function B, the value is 3
In function C, the value is 2
In function A, the value is 1

Complexity Analysis: The time complexity of the program is O(n), and the space complexity of the program is also O(n).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA