Finite and Infinite Recursion with examples
Last Updated :
27 Jan, 2023
The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function.
Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS, etc.
Types of Recursions:
Recursion can be further classified into two kinds, depending on when they terminate:
- Finite Recursion
- Infinite Recursion
Finite Recursion:
Finite Recursion occurs when the recursion terminates after a finite number of recursive calls. A recursion terminates only when a base condition is met.
Example:
Below is an implementation to demonstrate Finite Recursion.
C++
#include <bits/stdc++.h>
using namespace std;
void Geek( int N)
{
if (N == 0)
return ;
cout << N << " " ;
Geek(N - 1);
}
int main()
{
int N = 5;
Geek(N);
return 0;
}
|
Java
class GFG{
static void Geek( int N)
{
if (N == 0 )
return ;
System.out.println(N + " " );
Geek(N - 1 );
}
public static void main(String[] args)
{
int N = 5 ;
Geek(N);
}
}
|
Python3
def Geek( N):
if (N = = 0 ):
return
print ( N, end = " " )
Geek(N - 1 )
N = 5
Geek(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void Geek( int N)
{
if (N == 0)
return ;
Console.Write(N + " " );
Geek(N - 1);
}
public static void Main(String[] args)
{
int N = 5;
Geek(N);
}
}
|
Javascript
<script>
function Geek(N)
{
if (N == 0)
return ;
document.write(N + " " );
Geek(N - 1);
}
var N = 5;
Geek(N);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
The recursion tree for the above recursive function looks like this.
Recursion Tree
When the value of N becomes 0, because of the base condition, the recursion terminates.
Infinite Recursion:
Infinite Recursion occurs when the recursion does not terminate after a finite number of recursive calls. As the base condition is never met, the recursion carries on infinitely.
Example:
Below is an implementation to demonstrate Infinite Recursion.
C++
#include <bits/stdc++.h>
using namespace std;
void Geek( int N)
{
if (N == 0)
return ;
cout << N << " " ;
Geek(N);
}
int main()
{
int N = 5;
Geek(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void Geek( int N)
{
if (N == 0 )
return ;
System.out.print( N + " " );
Geek(N);
}
public static void main(String[] args)
{
int N = 5 ;
Geek(N);
}
}
|
Python3
def Geek(N):
if (N = = 0 ):
return
print (N, end = " " )
Geek(N)
N = 5
Geek(N)
|
C#
using System;
class GFG
{
static void Geek( int N)
{
if (N == 0)
return ;
Console.Write( N + " " );
Geek(N);
}
public static void Main(String[] args)
{
int N = 5;
Geek(N);
}
}
|
Javascript
<script>
function Geek(N)
{
if (N == 0)
return ;
document.write( N + " " );
Geek(N);
}
var N = 5;
Geek(N);
</script>
|
Time Complexity: non finite as this recursion will never end.
Auxiliary Space: non finite
The recursion tree for the above recursive function looks like this.
Recursion Tree
Since the value of N never becomes 0, so the recursion never terminates. Instead, the recursion continues until the implicit stack becomes full which results in a Stack Overflow. Some compilers directly give the output as Segmentation Fault (Core Dumped), while others may abnormally terminate for some value and then show Segmentation fault.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...