I recently learnt about tail recursion. I understand, many programming language compilers perform[Current java doesn't] s, code optimization when it finds a recursive method a "Tail recursive".
My understanding of TR : Compiler, does not create new stack frame(instead replace with older call's stack frame) when there is no further operation to be performed after the call has returned
With mycurrent understanding of TR, I think below is a "Tail recursive", because I am not doing any further operation, after the recursive method is returned
suppose totalSeriesLenght = 10.
public void generateFibonacciSeries(int totalSeriesLenght) {
int firstNum = 0;
int secondNum = 1;
printNextFibonacciNumber(firstNum, secondNum,totalSeriesLenght);
}
public void printNextFibonacciNumber(int fiboOne , int fiboTwo,int totalSeriesLenght) {
if(totalSeriesLenght >= 1) {
System.out.print(fiboOne + ",");
int fiboNext = fiboOne + fiboTwo;
totalSeriesLenght --;
printNextFibonacciNumber(fiboTwo, fiboNext,totalSeriesLenght);
}
}
No comments:
Post a Comment