AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • What Recursion Looks Like
  • Parts of a Recursive Method
  • Examples of Recursion
  • Factorials and Recursion:
  • Fibonacci Sequence and Recursion:
  • The Humor of Recursion

Was this helpful?

  1. Algorithms and Recursion

Advanced: Recursion

PreviousInsertion SortNextMergesort

Last updated 5 years ago

Was this helpful?

Recursion is when you break down a given problem into smaller problems of the same instance. The goal is to break down the problems into smaller forms so that they become easier to solve.

In computer science, recursion is when a method calls itself to solve a given problem.

What Recursion Looks Like

Recursion is an important part of Computer Science, because it allows us to condense our code into a simple, easy to read, form.

Parts of a Recursive Method

Base Case:

The Base Case is the terminating case in a recursive problem. This case does not use recursion, because it is the simplest form of the problem. The Base Case causes the method to terminate. If we don't have a Base Case the recursive method would continue indefinitely.

Recursive Case:

The Recursive Case is the step, or set of steps, that reduces all the other cases as the Recursive Algorithm gets closer to the Base Case.

Recursive Algorithm:

The Recursive Algorithm is a finite set of steps that calls itself with simpler inputs, as the algorithm approaches the Base Case.

Examples of Recursion

Some common examples of recursive solutions include Factorials and the Fibonacci Sequence. Other examples of recursive solutions include: Tower of Hanoi, Golden Ratio, Catalan Numbers, and Computer Compound Interest.

Factorials and Recursion:

One common way to solve for factorials use to use a for loop. Here is one example:

private int factorial(int n)
{
    int res = 1;
    for(int i = n; i > 0; i--)
    {
        res *= i;
    }
    return res;
}

While this solution definitely works, we can simply it using recursion. Looking at the formula for factorials: n * (n-1) * (n-2) * (n-3)...

we can see a recurrence relation of:

n!:   1 for n = 0,
      (n-1)! * n for n > 0

Looking at this from a programming point of view, this means: factorial(0); = 1, and factorial(n); = factorial(n-1) * n;

Base Case:

Since factorial(0); is the simplest form we can achieve, it is our base case. This means we want to keep calling our factorial(); method until the input is equal to 0.

Recursive Case:

Given that factorial(0); is our Base Case we can conclude that factorial(n) = factorial(n - 1) * n; is our Recursive Case.

Now that we have our Base Case and Recursive Case we can construct our recursive method:

private int factorial(int n)
{
    // Our Base Case
    if(n == 0)
    {
        return 1;
    }

    // Our Recursive Case
    return factorial(n - 1) * n;
}

Fibonacci Sequence and Recursion:

The Fibonacci Sequence is achieved by adding up the two previous numbers to get the next number. The sequence looks like: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

The recurrence relation for the Fibonacci Sequence is:

F(n) = F(n - 1) + F(n - 2)

Given this formula, and looking at the sequence we can conclude that F(0) = 1 and F(1) = 1

From a programming point of view, this means fibonacci(0) = 1, fibonacci(1) = 1, and fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2)

Base Case:

Since fibonacci(0) = 1 and fibonacci(1) = 1 are the simplest forms we can achieve, these are our Base Cases.

Recursive Case:

Now that we know our Base Cases, we are left with fibonacci(n) = fibonacci(n-1) + fibonacci(n-2). This is our Recursive Case.

Now that we have our Base Cases and Recursive Case we can construct our recursive method:

private int fibonacci(int num)
{
    // Our Base Cases
    if(num == 0 || num == 1)
    {
        return 1;
    }

    // Our Recursive Case
    return fibonacci(num-1) + fibonacci(num-2);
}

The Humor of Recursion

Sometimes recursion is used as the subject of humor for mathematicians and computer scientists. One of the most famed examples of humorous recursion can be seen by going to Google and searching: "recursion."

Source:

Source:

https://prateekvjoshi.com/2013/10/05/understanding-recursion-part-i/
https://en.wikipedia.org/wiki/Tower_of_Hanoi
Recursion Example
Recursion Tower
Google Recursion Humor