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

Recursion Presentation

The document provides a comprehensive guide to understanding recursion, defining it as a method where a function calls itself to solve smaller instances of a problem. It contrasts recursion with iteration, highlights the importance of base cases, and presents examples such as factorial and Fibonacci functions. Additionally, it discusses the pros and cons of recursion, common applications, and tips for writing recursive functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Recursion Presentation

The document provides a comprehensive guide to understanding recursion, defining it as a method where a function calls itself to solve smaller instances of a problem. It contrasts recursion with iteration, highlights the importance of base cases, and presents examples such as factorial and Fibonacci functions. Additionally, it discusses the pros and cons of recursion, common applications, and tips for writing recursive functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Understanding Recursion Made

Simple
A guide to mastering the concept
with examples
Introduction to Recursion
• Definition:
• Recursion is a method where the solution to a
problem depends on solving smaller instances
of the same problem.

• Key Feature:
• A function calls itself.
Recursion vs. Iteration
• Recursion:
• - Uses function calls.
• - Simplifies problems.

• Iteration:
• - Uses loops (for, while).
• - More memory-efficient.
Anatomy of a Recursive Function
• 1. Base Case: Stops recursion.
• 2. Recursive Case: Calls itself with smaller
input.
Example 1 - Factorial
• def factorial(n):
• if n == 0: # Base Case
• return 1
• else: # Recursive Case
• return n * factorial(n - 1)

• print(factorial(5)) # Output: 120


Flow of Recursive Calls (Factorial)
• factorial(5) → factorial(4) → factorial(3) →
factorial(2) → factorial(1) → factorial(0)
Example 2 - Fibonacci Sequence
• def fibonacci(n):
• if n <= 1: # Base Case
• return n
• else: # Recursive Case
• return fibonacci(n - 1) + fibonacci(n - 2)

• print(fibonacci(5)) # Output: 5
Visualization of Fibonacci
• Tree structure showing calls to fibonacci(4)
and fibonacci(3)
Importance of Base Case
• Without a base case, recursion leads to
infinite loops and stack overflow errors.

• Example:
• def infinite_recursion():
• infinite_recursion()

• infinite_recursion() # Results in an error


Common Applications of Recursion
• 1. Solving mathematical problems (factorial,
Fibonacci).
• 2. Data structures:
• - Traversing trees (binary trees).
• - Searching and sorting algorithms.
• 3. String manipulations.
Example 3 - Binary Search
• def binary_search(arr, target, low, high):
• if low > high: # Base Case
• return -1
• mid = (low + high) // 2
• if arr[mid] == target:
• return mid
• elif arr[mid] > target:
• return binary_search(arr, target, low, mid
- 1)
Recursive vs. Iterative Binary
Search
• Comparison of code length and readability.
• Efficiency: Both are O(log n).
Pros and Cons of Recursion
• Pros:
• - Simplifies complex problems.
• - Elegant and concise.

• Cons:
• - Higher memory usage.
• - Risk of stack overflow.
Tips for Writing Recursive
Functions
• 1. Clearly define the base case.
• 2. Ensure progress towards the base case.
• 3. Visualize the problem (e.g., recursion tree).
• 4. Use memoization for efficiency (dynamic
programming).
Conclusion
• Recursion is a powerful tool for problem-
solving.
• Practice is key to mastering it.
• Questions?

You might also like