0% found this document useful (0 votes)
5 views

Compter Recursion

Uploaded by

bindaladwit19
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Compter Recursion

Uploaded by

bindaladwit19
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Recursion

Recursion means: CALLING ONTO ITSELF


Function Recursion:
When a method is called inside its own definition(body) then it is
called recursion and the function is called recursive function.
OR
If both the calling & called function are same then it is called
recursion.

A recursive method repeatedly calls it self. Until it reaches to a


terminating condition.
It is suitable to replace looping statements like for, while loop etc.
Kedar
Recursive approach:
In recursive approach the function calls itself until
the condition is encounter.

It is slower than iteration, which means it uses more


memory than iteration.

recursion is like a selection structure, and which


makes code smaller and clean.(it means process of
solving a problem by reducing it to smaller versions
of itself.

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.

 It also has greater time requirements


because each time the function is called,
he stack grows and the final answer is
returned when the stack is popped
completely.
Kedar
Recursive Methods:
 A method in Java that calls itself is called recursive
method
 Recursion is a programming technique in which a
call to a method appears in that method’s
body(i.e., a method calls itself : this is called direct
recursion)
 It works on the principle of LIFO (Stack data
structure)

Note: Last memory opened will be the first memory


to be closed
Kedar
Parts of Recursive Methods:

 The two BASIC PARTS/ CASES OF RECURSIVE METHODS

Base case: Any pre-known value/ condition before


beginning a process/ calculation/task. It is used to
terminates the recursive process.

e.g. Any number to the power 0 is 1.(Pre-known case)

Recursive case: The repetitive condition of a recursive


method. Method call which can call itself.

Kedar
TYPES OF RECURSION:

If a function calls itself, it’s known as direct recursion.


(i.e. the function makes a recursive call inside its own
function body, it is one-step recursive call).
If the function func1() calls another function func2()
and func2() calls func1() then it is indirect recursion (or
mutual recursion) (This is a two-step recursive call: the
function calls another function to make a recursive call).

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)

void show(int n) // n=3


{ 0
if(n>0) 1
{ 2
show(--n);
System.out.println(n); // process done after recursive call
}
}
Kedar
CLASSIFICATIONS OF RECURSIVE METHODS
 TAIL RECURSIVE :
A method recursion , in which the last statement executed is
the recursive call. The processing occurs before the recursive
call.
void show(int n) // n=3 3
{ 2
if(n>0) 1
{
System.out.println(n); // process done before recursive call
show(--n);
}
}
Kedar
Recursion vs Iteration:
Difference between recursion and iteration-
Recursion and Iteration both are two
different programming approaches. In some
cases recursion is best suited and in some
other cases iterative way of programming is
good.
In programming, repeated set of instructions
can be handled either by using recursive or
iterative approach in your code. So which
approach you choose and why.
Kedar
Differences :
i) In recursion, function call itself until the base
condition is reached.
i) Iteration means repetition of process until the
condition fails. e.g. when you use loop (for,while etc.)
in your programs.

ii) In recursive function, only base condition


(terminate condition) is specified.

ii) Iterative approach involves four steps,


initialization,condition, execution and updation.
Kedar
Differences :
iii) Recursion keeps your code short and simple

iii) Iterative approach makes your code longer.

iv) Recursion is slower than iteration due to overhead


of maintaining stack (It is a slow process)
iv)Iteration is a fast process.

v) Recursion takes more memory


v) Iteration requires less memory
Kedar
void show(int n) // n=3 1 Kedar
{
if(n>0) // BASE CONDITION DRY RUN
{
System.out.println(n);
show(--n);
System.out.println(n); // pending task Output:
}} 3 ---- Round1
Base condition ( 0>0 ) FALSE 2 ---- Round2
1 ---- Round3
R3: n=1 0 show(0), pending-3
0 --- Pending3
R2: n=2 1 show(1), pending -2 1 --- Pending2
R1: n=3 2 show(2), pending-1 2 --- Pending1

You might also like