Related Articles

Related Articles

Tail Call Elimination
  • Difficulty Level : Medium
  • Last Updated : 18 Jan, 2021

We have discussed (in tail recursion) that a recursive function is tail recursive if recursive call is the last thing executed by the function. 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// An example of tail recursive function
void print(int n)
{
    if (n < 0) 
       return;
    cout << " " << n;
 
    // The last executed statement is recursive call
    print(n-1);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// An example of tail recursive function
static void print(int n)
{
    if (n < 0
       return;
       
      System.out.print(" " + n);
 
    // The last executed statement
    // is recursive call
    print(n - 1);
}
 
// This code is contributed by rutvik_56

chevron_right


We also discussed that a tail recursive is better than non-tail recursive as tail-recursion can be optimized by modern compilers. Modern compiler basically do tail call elimination to optimize the tail recursive code. 

If we take a closer look at above function, we can remove the last call with goto. Below are examples of tail call elimination.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Above code after tail call elimination
void print(int n)
{
start:
    if (n < 0)
       return;
    cout << " " << n;
 
    // Update parameters of recursive call
    // and replace recursive call with goto
    n = n-1
    goto start;
}

chevron_right


QuickSort : One more example 
QuickSort is also tail recursive (Note that MergeSort is not tail recursive, this is also one of the reason why QuickSort performs better) 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

/* 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);
    }
}
// See below link for complete running code

chevron_right


The above function can be replaced by following after tail call elimination. 



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

/* QuickSort after tail call elimination
  arr[] --> Array to be sorted,
  low  --> Starting index,
  high  --> Ending index */
void quickSort(int arr[], int low, int high)
{
start:
    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);
 
        // Update parameters of recursive call
        // and replace recursive call with goto
        low = pi+1;
        high = high;
        goto start;
    }
}
// See below link for complete running code

chevron_right


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 last goto statement.

Function stack frame management in Tail Call Elimination : 
Recursion uses 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 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, current frame can be modified as per the requirements of 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 )
This article is contributed by Dheeraj Jain. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :