Recursion
Recursion
When a function calls itself (either directly or indirectly) then it is called recursive
function and process is called recursion.”
Principles of Recursion
➢ Original Problem can be decompose into small size of same type of problem.
➢ Recursive call must reduce the size of problem.
➢ Base condition must be present.
➢ Base condition must be reachable.
In order for the definition not to be circular, it must have two properties:
➢ There must be certain arguments called base value for which the function doesn’t
refers to itself.
➢ Each time the function does refers to itself , the argument of the function must be
closer to a base value.
A recursive function with these two properties is also said to be well-defined.
Recursion is done with the help of stack data structure.
A stack is a linear data structure, a collection of items of the same type. In a stack,
the insertion and deletion of elements happen only at one endpoint. The behavior of a
stack is described as “Last In, First Out” (LIFO).
Stack operations:
1. Push
2. Pop
What is the difference between recursion and iteration
Recursion: A program is called recursive when an entity calls itself.
Iterative: when a program is call iterative when there is a loop.
Used when code size needs to Used when time complexity needs to
be small, and time complexity be balanced against an expanded
Usage is not an issue. code size.