Recursion

Recursion

Recursion is a programming technique in which a method (recursive method) calls itself with a simpler version of the same problem until it reaches a point where the problem can be solved without recursion. Really what it means is that it's a repetition of a procedure. It can be a very powerful tool for solving problems because it allows you to break down a complex problem into smaller, simpler pieces that can be solved individually, commonly used with advanced sorting algorithms and navigating trees.

Recursion can be a useful tool for solving problems, but it is important to understand how it works and to use it appropriately. It is often more efficient to use iterative (loop-based) approaches to solve problems, especially for problems with a known upper bound on the number of iterations. However, in some cases, recursion may be the most natural or elegant way to solve a problem, and it can be a valuable tool to have in your toolkit as a programmer.

public class Main{
    public static void main(String [] args){
    int result = add(10); // adds all numbers from 10 down to 0;
    System.out.println(result); // prints 55
    }
    // recursive method
    private static int add(int x){
        if(x == 0) return 0; // base case
        return x + add(x - 1); // recursive case
    }

}

Recursion can be a bit difficult to understand but I'll try my best to explain it using the code example above. This recursive method in particular adds all numbers from 10 down to 0.

Base case: The base case is a specific situation or condition that is used to stop the recursion of a recursive method. And in this example the if statement is the base case, it's important because it stops the method from going in an infinite loop. Recursive methods are methods that call themselves to solve a problem, and they typically use a base case to determine when to stop the recursion and return a result. So a base case is simply a rule that a recursive method needs to have in order to stop the method.

Recursive case: This is the part where the method calls itself. A recursive case occurs when a method calls itself as part of its execution. This can be used to solve problems by dividing them into smaller, simpler subproblems that can be solved recursively, eventually combining the results to get the final solution. It's important to ensure that the base case is properly defined and that the recursion terminates, or the program may run indefinitely. This can be achieved by properly designing the recursive case to bring the problem closer to the base case with each recursive call.

Let's try another example.

Here is an example of a recursive method that calculates the factorial of a given number:

// second recursive method example
private int factorial(int n) {
    if (n == 1) {
    return 1; // base case
    }
    return n * factorial(n - 1); // recursive case
}

This method works by using a base case of n == 1, which returns a value of 1. For all other values of n, the method calls itself with a value of n - 1, and multiplies the result by n. This process continues until the base case is reached, at which point the recursive calls will start returning and the final result will be computed.

For example, if you call factorial(4), the method will return 4 * factorial(3). The call to factorial(3) will return 3 * factorial(2), and the call to factorial(2) will return 2 * factorial(1). Finally, the call to factorial(1) will return 1, so the final result will be 4 * 3 * 2 * 1 = 24.

This was by far the hardest article I've written but I hope it all makes sense, if not then you can check out other multiple sources, they might have a better explanation. I just wrote this because of a Leetcode problem I was struggling to overcome... I think I can go take it on now!