Types of Recursion in Java

Types of Recursion in Java

In java recursion can be categorized as

  • Head recursion
  • Tail recursion

1 - Head recursion

A recursion is called "head recursion" simply when the call take place before other process take place inside the function

Example

public void head(int n) {
	if(n == 0)
		return;
	else 
		head(n-1); <---

	Sysout.out.println() ....
}






2 - Tail recursion

It’s the opposite—the processing occurs before the recursive call.  The method executes all the statements before jumping into the next recursive call

Example

public void tail(int n) {
	if(n == 1) 
		return ;
	else 
		System.out.println()
	...

   tail(-1); <---
	
}

Choosing between the two recursive styles may seem arbitrary, but the choice can make all the difference.


要查看或添加评论,请登录

Mohamed Habib ALOUI的更多文章

社区洞察

其他会员也浏览了