Skip to main content

Tail Recursion

Definition: A special form of recursion where the last operation of a function is a recursive call. The recursion may be optimized away by executing the call in the current stack frame and returning its result rather than creating a new stack frame.

• If a recursive function is calling itself and that recursive call is the last statement in a function, then it is called as tail recursion. 

• After that call, it will nor perform anything.

 • All the function will be performing on the calling time itself

 • If there is some function that need to be performed after its returning time, then it is not a tail function .


Example-:

public class Test{
public static void fun(int n){
  if(n>0){
   System.out.print(n);
   fun(n-1);
}
}
public static void main(String[] args){
    fun(5);
}
}

output-: 5 4 3 2 1



Tail Recursion v/s loops: 

• Tail recursion can easily convert into loops as its structure and syntax is almost same 

• In terms of time taken by both is the same, 0(n) 

• Space taken by tail is 0(n) whereas the space for loops is 0(1) 

• To conclude, if you are using tail recursions its better to convert it into loop as the space used is less 

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Operators & Assignment part 3

                                                                                       Operators & Assignment 1-:instanceof Operators 2-:Bitwise Operators                                                                        instanceof Operators we can use instanceof operator to check whether the given object is of a particular type are not. Example-: /* list is a collection of objects. List l=new List(); l.add(Customer); l.add(Student); l.add(Test); Object o=l.get(...