Tail recursion occurs when a recursive function makes a recursive call as its last operation. There is no need to retain the previous state of the function, making it more memory efficient.
The time complexity of tail recursion is similar to that of non-tail recursion, generally O(n) for linear recursion. However, optimization like tail call optimization (TCO) can reduce the space complexity to O(1).
With tail call optimization, space complexity can be reduced to O(1) as it reuses the current stack frame for the next recursive call.
Step 1: Diagram of a function call stack in tail recursion. Step 2: Each call reuses the current stack frame. Step 3: Final result is returned after the last call. Step 4: No additional stack frames are needed.
public class TailRecursionExample {
public static void main(String[] args) {
System.out.println(factorial(5, 1));
}
public static int factorial(int n, int a) {
if (n == 0) return a;
return factorial(n - 1, n * a);
}
}
In the example above, the factorial function is tail recursive because the recursive call to factorial is the last thing executed by the function. The accumulator 'a' is used to carry the result.
Console Output:
120
A function calls itself directly. Example: factorial, Fibonacci series.
A function calls another function, which in turn calls the first function. This can involve more than two functions.
The recursive call is the last statement in the function. Optimized for space.
The recursive call is not the last statement in the function. Requires additional stack space.
Each function makes at most one recursive call. Example: factorial.
Each function makes multiple recursive calls. Example: Fibonacci series.
Direct recursion occurs when a function calls itself directly. It is the simplest form of recursion.
public class DirectRecursion {
public static void main(String[] args) {
System.out.println(sum(5));
}
public static int sum(int n) {
if (n <= 0) return 0;
return n + sum(n - 1);
}
}
This example calculates the sum of numbers from 1 to 5 using direct recursion. The function calls itself with a decremented value until it reaches the base case.
Console Output:
15
Indirect recursion involves two or more functions calling each other in a circular manner.
public class IndirectRecursion {
public static void main(String[] args) {
functionA(20);
}
public static void functionA(int n) {
if (n > 0) {
System.out.println("A: " + n);
functionB(n - 1);
}
}
public static void functionB(int n) {
if (n > 0) {
System.out.println("B: " + n);
functionA(n / 2);
}
}
}
In this example, functionA calls functionB, which in turn calls functionA. The recursion continues until the base condition is met.
Console Output:
A: 20
B: 19
A: 9
B: 8
A: 4
B: 3
A: 1
Non-tail recursion means the recursive call is not the last operation in the function, requiring additional operations after the call returns.
public class NonTailRecursion {
public static void main(String[] args) {
System.out.println(fibonacci(5));
}
public static int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
This example demonstrates a non-tail recursive function to calculate Fibonacci numbers. The function requires additional operations after the recursive calls return.
Console Output:
5
Linear recursion involves a function making a single recursive call per execution, creating a linear sequence of calls.
public class LinearRecursion {
public static void main(String[] args) {
printNumbers(5);
}
public static void printNumbers(int n) {
if (n <= 0) return;
System.out.println(n);
printNumbers(n - 1);
}
}
This example prints numbers from 5 to 1 using linear recursion, as each function call results in exactly one further call.
Console Output:
5
4
3
2
1
Tree recursion occurs when a function makes multiple recursive calls, leading to a tree-like structure of calls.
public class TreeRecursion {
public static void main(String[] args) {
System.out.println(fibonacci(5));
}
public static int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
This example calculates Fibonacci numbers using tree recursion, where each call to the function results in two further calls.
Console Output:
5
Mutual recursion involves two or more functions calling each other in a cyclical manner, often used in parsing and grammar checks.
public class MutualRecursion {
public static void main(String[] args) {
System.out.println(isEven(4));
}
public static boolean isEven(int n) {
if (n == 0) return true;
return isOdd(n - 1);
}
public static boolean isOdd(int n) {
if (n == 0) return false;
return isEven(n - 1);
}
}
This example checks if a number is even or odd using mutual recursion, where isEven calls isOdd and vice versa.
Console Output:
true
Nested recursion occurs when a recursive function calls itself with the result of another recursive call.
public class NestedRecursion {
public static void main(String[] args) {
System.out.println(nestedRecursion(95));
}
public static int nestedRecursion(int n) {
if (n > 100) return n - 10;
return nestedRecursion(nestedRecursion(n + 11));
}
}
This example demonstrates nested recursion, where the function calls itself with the result of another call to itself, creating complex recursion patterns.
Console Output:
91
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies