Skip to main content

Head Recursion

Now, let us understand Head Recursion. Following is the structure of head recursion. If there is a function that is calling itself, then it is a recursive function. As you can see in the below example, the function fun calling itself, so it is a recursive function. Then, if you further notice, the first statement inside the function is the recursive call. That means, what all the processing it has to do, it is doing at returning time, i.e. after the recursive call. There is no statement that is no operation before the function call.

Void fun( int n ) { 

if( n > 0) {
 fun(n-1); // head recursion 
 statement1
 statement2
 ……….. 
 }
 }

Note: If there is something before the recursive call then it is not a head recursion. If something is there before the function call, it is just a recursion. We don’t have to give any special name for it. The following is not head recursion.

Void fun( int n ) { 

if( n > 0) {
statement;
 ……….. 
 fun(n-1); //call function itself
 statement1;
 statement2;
 ……….. 
 }
 }

• Here the first statement inside the function is recursive call and all the processing of this function will be done after that.
• In head recursion the function doesn't have to process any operation at the time of function calling it has to do everything at the time of returning such functions are head recursion.

Head Recursion to Loop 
• Although it is possible but It is difficult to convert head recursion into a loop function.

Example-:

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

output-: 1 2 3 4 5


Note: The point that you need to remember is if a recursive function does some operation at returning, then it is not easy to convert that recursive function to a loop. We need to convert by writing some logic.

Time Complexity: The time complexity of Head Recursion is O(n).

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(...