Introduction to Recursion

A visual representation and explanation of how recursion works.

I vividly remember the day in college where my Introduction to Data Structures professor introduced the concept of recursion. And my brain promptly exploded.

This was such a radical idea and I felt like a potato with legs trying to figure out what the hell was going on.

Yes, recursion can be an absolute headache to think about… but once you draw it out, the solution becomes a lot clearer. So today, I’m going to break down a simple recursion problem in the hopes that you too can master this complex subject.

What Is Recursion?

Recursion is “a method of solving a problem where the solution depends on solutions to smaller instances of the same problem (as opposed to iteration).”

Recursion Example #1: The Taco Stand

Suppose you’re waiting in line to buy a taco, but unfortunately you can’t see how many people are in line in-front of you. You could walk to the front of the line and count the number of people (iterative). This is a pain in the ass because it requires you to leave your spot in line, and you’re hangry.

Instead, you could ask the person in front of you how many people are in line before them. They don’t have the answer, so they ask the person before them how many people are in-front of them in line.

This pattern continues until the question reaches the front of the line and finally the person being asked says “there are zero people in front of me.” Each person in line will then turn around and tell their neighbor the number of people before them, plus one. This continues until the final answer reaches you.

The JavaScript code might look something like this:

function findPlaceInLine(numberOfPeopleInLine) {

if(numberOfPeopleInLine === 1) {

return 0;

}

return 1 + findPlaceInLine(numberOfPeopleInLine-1);

}

And refactored for succinctness:

function findPlaceInLine(numberOfPeopleInLine) {

return numberOfPeopleInLine === 1 ? 0 : 1 + findPlaceInLine(numberOfPeopleInLine-1);

}

You might be wondering… okay but both of these solutions require you to go through the entire line to get an answer, so there is no benefit to using recursion here.

You are absolutely right. But there are use-cases where recursion can greatly surpass an iterative solution.

Recursion Example #2: The Address Book

Let’s say you’re getting married. You’ve sent out all of your invitations, and received all of the responses back except one: Aunt Sharon.

Aunt Sharon is notorious for being shady… so you decide to call her up and demand to know her response.

You pull out your address book and have two choices. You could start at the first page and flip through every single page until you find her contact information. This could be extremely time consuming if Aunt Sharon’s last name begins with a Z; or it could be efficient if her last name begins with an A. Eh… I don’t feel like hedging my bets (even though I know Aunt Sharon’s last name… work with me here.)

Instead, you decide to flip to the middle of the book and use the “Binary Search Algorithm” to look her up.

I’m not going to delve too deeply into algorithms, but the premise of the Binary Search Algorithm is that it grabs the middle element in a sorted array and compares the target value to the middle element. If the target value and middle element are equal, return the middle element (woohoo!) If the target is lower than the middle, recurse on the lower half of the array, otherwise recurse on the upper half (and discard the middle element).

Since Aunt Sharon’s last name is Flaky, and since there are 26 letters of the alphabet, we start at the letter “M” (I rounded down since there are an even number of letters). Since “F” comes before M, we do the same thing but using the letters A to L as the new “array”. The process looks something like this:

letters = [A - Z]

target = F

middle = M target < M

// recurse on lower half letters = [A - L]

middle = F

target = middle

FOUND!

By using recursion, we found Aunt Sharon’s name extremely quickly! Unfortunately the crazy woman forgot she bought tickets to “Kitty-Expo” for the same day, so she won’t be attending your nuptials.