Skip to content

Added Recursion topic in DSA #1012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/dsa/recursion/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "Recursion",
"position": 10,
"link": {
"type": "generated-index",
"description": "In data structures, The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using a recursive algorithm, certain problems can be solved quite easily. "
}
}
249 changes: 249 additions & 0 deletions docs/dsa/recursion/recursion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
---
id: recursion-in-dsa
title: Recursion in Data Structures and Algorithms
sidebar_label: Recursion
sidebar_position: 3
description: "Recursion is a programming technique where a function calls itself directly or indirectly to solve a problem. It is used in various applications such as solving mathematical problems, implementing data structures, and more."
tags:
[
dsa,
algorithms,
recursion,
recursive-functions,
recursion-in-dsa,
recursion-in-algorithm,
recursion-in-dsa-example,
recursion-in-dsa-explanation,
recursion-in-dsa-conclusion,
recursion-in-dsa-importance,
recursion-in-dsa-syntax,
recursion-in-dsa-implementation,
recursion-in-dsa-applications,
recursion-in-dsa-problems,
recursion-in-dsa-solutions,
recursion-in-dsa-code,
recursion-in-dsa-js,
recursion-in-dsa-java,
recursion-in-dsa-python,
recursion-in-dsa-c,
recursion-in-dsa-cpp,
recursion-in-dsa-ts,
]
---

Recursion is a programming technique where a function calls itself directly or indirectly to solve a problem. It is used in various applications such as solving mathematical problems, implementing data structures, and more.

## Why is Recursion important?

Recursion is important because it provides a simple and elegant way to solve complex problems. It allows problems to be broken down into smaller, more manageable subproblems, making it easier to understand and implement solutions.

## How to implement Recursion?

Recursion can be implemented in various programming languages using the following syntax:

## Recursive Function Structure

```python
def recursive_function(parameters):
if base_condition(parameters):
return base_case_value
else:
return recursive_function(modified_parameters)
```

## Examples of Recursive Functions

### Factorial

<Tabs>
<TabItem value="js" label="JavaScript" default>
<SolutionAuthor name="@Ajay-Dhangar"/>
```js
function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}
```
</TabItem>
<TabItem value="java" label="Java">
<SolutionAuthor name="@Ajay-Dhangar"/>
```java
public class RecursionExample {
public static int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
}
```
</TabItem>
<TabItem value="python" label="Python">
<SolutionAuthor name="@Ajay-Dhangar"/>
```python
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
```
</TabItem>
<TabItem value="c" label="C">
<SolutionAuthor name="@Ajay-Dhangar"/>
```c
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
```
</TabItem>
<TabItem value="cpp" label="C++">
<SolutionAuthor name="@Ajay-Dhangar"/>
```cpp
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
```
</TabItem>
<TabItem value="ts" label="TypeScript">
<SolutionAuthor name="@Ajay-Dhangar"/>
```ts
function factorial(n: number): number {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}
```
</TabItem>
</Tabs>
Fibonacci
<Tabs>
<TabItem value="js" label="JavaScript" default>
<SolutionAuthor name="@Ajay-Dhangar"/>
```js
function fibonacci(n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
```
</TabItem>
<TabItem value="java" label="Java">
<SolutionAuthor name="@Ajay-Dhangar"/>
```java
public class RecursionExample {
public static int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
```
</TabItem>
<TabItem value="python" label="Python">
<SolutionAuthor name="@Ajay-Dhangar"/>
```python
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
```
</TabItem>
<TabItem value="c" label="C">
<SolutionAuthor name="@Ajay-Dhangar"/>
```c
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
```
</TabItem>
<TabItem value="cpp" label="C++">
<SolutionAuthor name="@Ajay-Dhangar"/>
```cpp
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
```
</TabItem>
<TabItem value="ts" label="TypeScript">
<SolutionAuthor name="@Ajay-Dhangar"/>
```ts
function fibonacci(n: number): number {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
```
</TabItem>
</Tabs>

## Applications of Recursion

1. Mathematical computations: Factorial, Fibonacci series, power functions, etc.
2. Data structures: Tree and graph traversals.
3. Algorithm design: Divide and conquer algorithms like merge sort and quicksort.
4. Dynamic programming: Recursive definitions with memoization.
5. Common Recursive Problems
6. Tower of Hanoi: Moving disks between rods following specific rules.
7. Permutations and combinations: Generating all possible arrangements or selections.
8. Backtracking: Solving puzzles and constraint satisfaction problems.

## Advanced Recursive Techniques

1. Tail Recursion: A special form of recursion where the recursive call is the last statement in the function. Optimized by compilers to prevent stack overflow.
2. Memoization: Caching the results of expensive function calls to improve performance.

## Resources and References

### Books:

1. "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein
2. "The Art of Computer Programming" by Donald Knuth

### Online Courses:

1. Coursera: Data Structures and Algorithms Specialization
2. edX: Algorithms and Data Structures

### Websites:

1. GeeksforGeeks
2. LeetCode
3. HackerRank

By understanding and mastering these recursion concepts and techniques, you will be well-equipped to tackle a wide range of problems in data structures and algorithms.

## Conclusion

Recursion is a powerful technique in computer science that enables elegant and efficient solutions to complex problems. This guide covers the essential concepts and applications of recursion, providing a comprehensive understanding of how to implement and utilize recursive functions in different programming languages.

Understanding recursion is crucial for solving numerous problems in computer science, from basic mathematical computations to complex algorithm design. The examples provided demonstrate how to work with recursion effectively, ensuring a robust foundation for tackling more advanced DSA concepts. Mastery of recursion enhances your ability to handle algorithmic challenges and perform operations crucial in both everyday programming and competitive coding.

## Problems for Practice

To further practice and test your understanding of recursion, consider solving the following problems from LeetCode:

1. Fibonacci Number
2. Climbing Stairs
3. Permutations
4. Subsets
5. Combination Sum


Engaging with these problems will help reinforce the concepts learned and provide practical experience in using recursion effectively. By practicing these problems, you will enhance your problem-solving skills and deepen your understanding of recursive algorithms in various contexts.
Loading
Loading