Introduction To Data Structure - Recursion
Introduction To Data Structure - Recursion
Iteration Recursion
• Iteration is a process of • Recursion is a technique that
executing a block of statements breaks down a problem into one
repeatedly until some specific or more subproblems that are
condition similar to the original problem
• The iterative process is more • Recursion is not efficient in
efficient in terms of storage terms of storage space and
space and execution time. execution time.
• Any recursive problem can be • Not all problems have a
solved iteratively. recursive solution
Iteration Recursion
• Iteration process sometimes not • Recursive functions are easier to
easy to implement. Complicated implement and maintain.
problems are difficult to solve in Complicated problems are
iteratively; e.g. tower of Hanoi solved easily.
problem, eight queen problem • A recursive function must have
etc. base criteria for which the
• Iteration has four steps: function does not call itself. Each
initialization, condition checking, time a function does call itself
execution statements and (directly or indirectly); it must be
updating. closer to the base criteria.
Dr. Anand Kumar Mishra
When should we use iteration, and when use
recursion?
• There are three factors to consider:
1. Iterative functions are typically faster than their recursive alternatives.
Therefore, if speed were an issue, you would normally use iteration
2. If the stack limit is too constraining then you will prefer iteration over
recursion
3. Some procedures are very naturally programmed recursively, and all but
unmanageable iteratively. Here, the choice is clear.
Therefore, in Stack first fact (3) is pushed, then fact (2) and at last, the fact
(1) is pushed. Moreover, these function calls are processed according to
the sequence they are popped from the stack.