My Definition: Recursion is a problem solving method by continuously breaking it down into smaller instances that are trivial to solve. This is done through a function defined as smaller instances of itself.
Wikipedia: Recursion in computer programming is exemplified when a function is defined in terms of simpler, often smaller version of itself.
Let's take a look at an example to illustrate it.
Fibonacci Numbers
Similar to factorials, Fibonacci numbers are defined as Fn=Fn−1+Fn−2 where Fn is the nth Fibonacci number for n>1 and F0=0 and F1=1. Here's the code for it.
intfibonacci(int n){
if (n == 0) { // Small, trivial case, i.e. base case.return0;
} elseif (n == 1) { // Small, trivial case, i.e. base case.return1;
} else { // Recursive case.return fibonacci(n - 1) + fibonacci(n - 2);
}
}
Let's walk through it. Does that make sense?
Now, let's take a look at the iterative approach.
Fibonacci Iterative Approach
intfibonacci(int n){
if (n == 0) {
return0;
} elseif (n == 1) {
return1;
} else { // Gross...int nMinusOne = 1;
int nMinusTwo = 0;
int value = 1;
for (int i = 2; i < n; i++) {
int newValue = value + nMinusOne;
nMinusTwo = nMinusOne;
nMinusOne = value;
value = newValue;
}
return value;
}
}
Brief Review Over
I got like three off-by-one errors dealing with that iterative approach. Just like real life tools, there may be some instances where recursion is more elegant to solve a problem. There are going to be problems that match well with recursion. You'll get that intuition soon.
What are you still feeling uncomfortable with? Literally anything.
Don't worry. Recursion is difficult.
Searching Through a Linked List Recursively (15 min.)
To review, here's the iterative approach.
booleancontains(int value){
for (Item current = this.start; current != null; current = current.next) {
if (current.value == value) {
returntrue;
}
}
returnfalse;
}
I want you to solve this through recursion, I'll give you the pseudocode and starter code.
Linked List Starter Code
classLinkedList{
classItem{
int value;
Item next;
Item(int setValue, Item setNext) {
this.value = setValue;
this.next = setNext;
}
}
Item start;
voidadd(int value){
Item i = new Item(value, null);
if (this.start == null) {
this.start = i;
} else {
i.next = this.start;
this.start = i;
}
}
@Overridepublic String toString(){
String buffer = "";
for (Item i = this.start; i != null; i = i.next) {
buffer += i.value + " -> ";
}
return buffer + "null";
}
}
LinkedList ls = new LinkedList();
ls.add(1);
ls.add(2);
ls.add(3);
System.out.println(ls);
Pseudocode for Recursive Contains
// Public facing function.publicbooleancontains(int value){
return containsHelper(value, this.start);
}
// Recursive function.privatebooleancontainsHelper(int value, Item start){
if (<Is it here?>) {
returntrue;
} else {
return <Check the rest of the list.>
}
}
Similar to Fibonacci numbers, Tribonacci numbers are defined as the sum of the previous three Tribonacci numbers where Tribonacci number n when n>3 is defined as Tn=Tn−1+Tn−2+Tn−3 where T0=T1=0 and T2=1.
Feel free to mimic this off of the Fibonacci number implementation previously.
You can do it :)
Here's the sequence if you want a reference. Index starts at zero.
I hope recursion is a little less scary after today.
You'll learn to have Stockholm syndrome for recursion soon.
You're now in the know of about 50% of programming jokes, higher on r/programmerhumor. Welcome to the club!
As always, have a safe and awesome weekend. You're almost to summer, hang in there.
Solution Section
Recursive Contains Solution
// Public facing function.publicbooleancontains(int value){
return containsHelper(value, this.start);
}
// Recursive function.privatebooleancontainsHelper(int num, Item node){
if (node == null) { // We reached the end of the linked list.returnfalse;
}
if (node.value == num) { // Check here.returntrue;
} else {
return containsHelper(num, node.next);
}
}
Broken Array Sum Solution
import java.util.ArrayList;
import java.util.List;
intsum(List<Integer> arr){
if (arr.size() == 1) {
return arr.get(0);
} else {
return arr.get(0) + sum(arr.subList(1, arr.size()));
}
}
List<Integer> arr = new ArrayList<>();
arr.add(1);
arr.add(2);
arr.add(3);
System.out.println(sum(arr)); // Should be six.
Tribonacci Number Solution
inttribonacci(int n){
if (n == 0 || n == 1) { // Small, trivial case, i.e. base case.return0;
} elseif (n == 2) { // Small, trivial case, i.e. base case.return1;
} else { // Recursive case.return tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3);
}
}