We have discussed (in tail recursion) that a recursive function is tail recursive if the recursive call is the last thing executed by the function.
We also discussed that a tail-recursive is better than a non-tail recursive as tail-recursion can be optimized by modern compilers. Modern compiler basically does tail call elimination to optimize the tail-recursive code.
If we take a closer look at the above function, we can remove the last call with goto. Below are examples of tail call elimination.
C++
// Non-tail recursive function
void print(int n) {
if (n < 0)
return;
// Recursive call happens first (not last)
print(n - 1);
// Now process the current node
cout << " " << n;
}
Java
static void print(int n) {
if (n < 0)
return;
// Recursive call happens first
print(n - 1);
// Process after recursion
System.out.print(" " + n);
}
Python
def print_n(n):
if n < 0:
return
# Recursive call first
print_n(n - 1)
# Process after recursion
print(n, end=" ")
C#
static void Print(int n) {
if (n < 0) return;
// Recursive call first
Print(n - 1);
// Process after recursion
Console.Write(n + " ");
}
JavaScript
function printN(n) {
if (n < 0) return;
// Recursive call first
printN(n - 1);
// Process after recursion
process.stdout.write(n + " ");
}
QuickSort : One more example
QuickSort is also tail recursive (Note that MergeSort is not tail recursive, this is also one of the reasons why QuickSort performs better)
C++
/* Tail recursive function for QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
Java
/* Java program to implement the above approach
Tail recursive function for quick sort
arr[] --> to get the elements
low --> starting index
high --> ending index
*/
static void quickSort(int[] arr, int low, int high)
{
if (low < high)
{
/* pi is partitioning index,after the call
to partition function the arr[p] will be
at its correct index*/
int pi = partition(arr, low, high);
// Sort elements before and after
// the partition separately
// thus whole array will get sorted eventually
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
Python
# Python program to implement
# the above approach
# Tail recursive function for QuickSort
# arr[] --> Array to be sorted,
# low --> Starting index,
# high --> Ending index */
def quickSort(arr,low,high):
if(low<high):
# pi is partitioning index, arr[p] is now
# at right place
pi=partition(arr,low,high)
# Separately sort elements before
# partition and after partition
quickSort(arr,low,pi-1)
quickSort(arr,pi+1,high)
C#
// C# program to implement
// the above approach
using System;
class GFG{
/* Tail recursive function for QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
static void quickSort(int[] arr, int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
}
JavaScript
/* Tail recursive function for QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
function quickSort( arr, low, high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
let pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
The above function can be replaced by following after tail call elimination.
C++
// QuickSort after tail call elimination
void quickSort(int arr[], int low, int high) {
while (low < high) {
int pi = partition(arr, low, high);
// Recursively sort left part
quickSort(arr, low, pi - 1);
// Tail recursion elimination for right part
low = pi + 1;
}
}
Java
// QuickSort after tail call elimination
static void quickSort(int arr[], int low, int high) {
while (low < high) {
int pi = partition(arr, low, high);
// Sort left part
quickSort(arr, low, pi - 1);
// Tail recursion elimination for right part
low = pi + 1;
}
}
Python
# QuickSort after tail call elimination
def quick_sort(arr, low, high):
while low < high:
pi = partition(arr, low, high)
# Sort left part
quick_sort(arr, low, pi - 1)
# Tail recursion elimination for right part
low = pi + 1
C#
// QuickSort after tail call elimination
static void QuickSort(int[] arr, int low, int high) {
while (low < high) {
int pi = Partition(arr, low, high);
// Sort left part
QuickSort(arr, low, pi - 1);
// Tail recursion elimination for right part
low = pi + 1;
}
}
JavaScript
// QuickSort after tail call elimination
function quickSort(arr, low, high) {
while (low < high) {
let pi = partition(arr, low, high);
// Sort left part
quickSort(arr, low, pi - 1);
// Tail recursion elimination for right part
low = pi + 1;
}
}
Therefore job for compilers is to identify tail recursion, add a label at the beginning and update parameter(s) at the end followed by adding the last goto statement.
Function stack frame management in Tail Call Elimination :
Recursion uses a stack to keep track of function calls. With every function call, a new frame is pushed onto the stack which contains local variables and data of that call. Let's say one stack frame requires O(1) i.e, constant memory space, then for N recursive call memory required would be O(N).
Tail call elimination reduces the space complexity of recursion from O(N) to O(1). As function call is eliminated, no new stack frames are created and the function is executed in constant memory space.
It is possible for the function to execute in constant memory space because, in tail recursive function, there are no statements after call statement so preserving state and frame of parent function is not required. Child function is called and finishes immediately, it doesn't have to return control back to the parent function.
As no computation is performed on the returned value and no statements are left for execution, the current frame can be modified as per the requirements of the current function call. So there is no need to preserve stack frames of previous function calls and function executes in constant memory space. This makes tail recursion faster and memory-friendly.
Next Article:
QuickSort Tail Call Optimization (Reducing worst case space to Log n )
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem