Compter Recursion
Compter Recursion
Kedar
Advantages :
For a recursive function, we only need to
define the base case and recursive case, so
the code is simpler and shorter than an
iterative code
Some problems are inherently recursive,
such Graph and Tree Traversal
Kedar
Disadvantages:
A recursive program has greater space
requirements than an iterative program as
each function call will remain in the stack
until the base case in reached.
Kedar
TYPES OF RECURSION:
Kedar
TYPES OF RECURSION:
DIRECT RECURSION INDIRECT RECURSION
void show() void show()
{
{ …..
….. calc();
…. ……
}
show();
void calc()
} {
……..
show();
…
Kedar }
CLASSIFICATIONS OF RECURSIVE METHODS
FINITE RECURSIVE :
A recursive method having one or more base
cases and is reachable or attainable.
void show(int n) // n=3 3
2
{
1
if(n>0) 0
{ 1
System.out.println(n); 2
show(--n);
System.out.println(n); // pending task
}} Kedar
CLASSIFICATIONS OF RECURSIVE METHODS
INFINITE RECURSIVE :
A recursive method which has no base case OR
the base case is not reachable or attainable.
void show(int n) // n=3 3
{ 3
if(n>0) 3
{ ..
Never ending
System.out.println(n);
show(n--);
System.out.println(n); // pending task
}} Kedar
CLASSIFICATIONS OF RECURSIVE METHODS
AUGMENTED RECURSIVE :
In augmented recursion , the recursive call, when it happens,
comes before other processing in the function(It means
recursive call is set at the top, or head of the function)