Head recursion Vs Tail recursion

What is Recursion?

A recursion is a function which calls itself directly or indirectly over a defined boundary to generate user expected results.

Some common problems for recursion are Fibonacci series, Factorial of an integer and Tower of …


This content originally appeared on DEV Community and was authored by soorya54

What is Recursion?

A recursion is a function which calls itself directly or indirectly over a defined boundary to generate user expected results.

Some common problems for recursion are Fibonacci series, Factorial of an integer and Tower of Hanoi

Recursion Example

#include <stdio.h>

// T(n) = Θ(n)
// Aux space = Θ(n)

int getFactorial(int n) {
    if(n==0 || n==1)
        return 1;
    return n*getFactorial(n-1);
}

int main() {
    int n, res;
    scanf("%d", &n);

    res = getFactorial(n);
    printf("%d", res);

    return 0;
}

Test case

Input
4

Output
24

Head recursion

If a recursion has code statements to be executed after function call then it is a Head recursion. Head recursions are generally hard to convert into loop statements.

Example

void fun(int n) {
    if(n==0)
        return 0;

    fun(n-1);
    printf("%d", n); // Post recursive operation
}

Tail recursion

Tail recursions will not have any code statements after function calls and are generally at the end of the function declaration. Tail recursions are easy to convert into loop statements.

Example

void fun(int n) {
    if(n==0)
        return 0;

    printf("%d", n); 
    fun(n-1);
}

Which is better?

Generally, tail recursions are always better. Even though they both have same time complexity and Auxiliary space, tail recursions takes an edge in terms of memory in function stack. Head recursions will wait in function stack memory until the post recursion code statements are executed which causes a latency in overall results, whereas tail recursions will be terminated in function stack over execution.

Alt Text

That's it

Thanks for reading!! If you have any questions about the post feel free to leave a comment below.

Follow me on twitter: @soorya_54


This content originally appeared on DEV Community and was authored by soorya54


Print Share Comment Cite Upload Translate
APA
soorya54 | Sciencx (2023-12-04T10:21:27+00:00) » Head recursion Vs Tail recursion. Retrieved from https://www.scien.cx/2021/05/03/head-recursion-vs-tail-recursion/.
MLA
" » Head recursion Vs Tail recursion." soorya54 | Sciencx - Monday May 3, 2021, https://www.scien.cx/2021/05/03/head-recursion-vs-tail-recursion/
HARVARD
soorya54 | Sciencx Monday May 3, 2021 » Head recursion Vs Tail recursion., viewed 2023-12-04T10:21:27+00:00,<https://www.scien.cx/2021/05/03/head-recursion-vs-tail-recursion/>
VANCOUVER
soorya54 | Sciencx - » Head recursion Vs Tail recursion. [Internet]. [Accessed 2023-12-04T10:21:27+00:00]. Available from: https://www.scien.cx/2021/05/03/head-recursion-vs-tail-recursion/
CHICAGO
" » Head recursion Vs Tail recursion." soorya54 | Sciencx - Accessed 2023-12-04T10:21:27+00:00. https://www.scien.cx/2021/05/03/head-recursion-vs-tail-recursion/
IEEE
" » Head recursion Vs Tail recursion." soorya54 | Sciencx [Online]. Available: https://www.scien.cx/2021/05/03/head-recursion-vs-tail-recursion/. [Accessed: 2023-12-04T10:21:27+00:00]
rf:citation
» Head recursion Vs Tail recursion | soorya54 | Sciencx | https://www.scien.cx/2021/05/03/head-recursion-vs-tail-recursion/ | 2023-12-04T10:21:27+00:00
https://github.com/addpipe/simple-recorderjs-demo