RECURSION
PAUL LEANDRO LANOT
COLLEGE LECTURER, SICS
WHAT IS RECURSION?
Recursion is a way of solving a problem
by having a function calling itself
EXAMPLE IS A RUSSIAN DOLL
WHAT IS RECURSION?
Recursion is a way of solving a problem by having a function calling itself
Performing the same operation multiple times with
different inputs.
In every step we try smaller inputs to make the problem
smaller.
Base condition is needed to stop the recursion,
otherwise infinite loop will occur.
RUSSIAN DOLL IN PROGRAMMING
WHY RECURSION?
Recursive thinking is really important in programming and it helps you break down big problems into
smaller ones and easier to use
When to choose recursion?
• If you can divine the problem into similar sub problems
• Design an algorithm to compute nth
• Write code to list the n
• Implement a method to compute all.
• Practice
The prominent usage of recursion in data structures like trees and graphs
It is used in many algorithms (divide and conquer, greedy and dynamic programming)
HOW RECURSION WORKS?
1. A method calls it self
2. Exit from infinite loop
HOW RECURSION WORKS INTERNALLY