Using Recursion through an object tree

When I first learned about recursion, back in remote prep I was advised to not worry too much at the time, with it. BIG MISTAKE!! Luckily I have grasped the knowledge of recursion but I am still on a basic level. Week 1 of Hack Reactor was brutal with data structures.

In this blog I will give a basic rundown on how recursion works with through a tree.

The data structure type known as a tree, can have n numbers of levels, known as depth. You can not solve a problem without recursion for this. It’s just the hard facts that you need to understand, so let’s start!

This problem will be for a Farm Ranch and you need to find and store every farm owner based off of age.

var farmRanch = {
owner : 'Farmer John',
age : 45,
branch : [
{
owner : 'Farmer Jill',
age : 36,
branch : [
owner : 'Farmer Rick',
age : 89,
branch : []
]
},
{
owner : 'Farmer Judith',
age : 16,
branch : [
{
owner : 'Farmer Pear',
age : 26,
branch : []
}
]
}
]
};

Our function will take in a object and a test, and we will want to store our results in an array once we reach our goal.

function findFarmerAge(farmer, ageTest) {
// write code here
}

We need to find the smallest type of data to work with and build our solution up until we get back what we were asked to retrieve.

For example, the test could ask for all owners who are between the age of 20 and 40.

Let’s create a test that does that!

var middleFarmers = function(farmerObject) {
return farmerObject.age >= 20 && <= 40;
};

So we have a callback function that accepts a object, and will return the property of that object, if it’s true.

Before you start coding, let’s pseudocode our thought out, it will save you a lot of time.

//function findFarmerAge(farmer, ageTest) {
// a variable to store our final result, an array will work
// recursive function that takes in a object
// base case
// if the farmer's age passes the test
// you can store their name in the array
// return from this if statement to continue retrieving
// create a reference of the branch array
// loop through the branch with your recursive function
// run the function taking in the branch's objects
//
// initial start of your recursive function
// return your final result

And done with pseudocode!

Make sure you look over and understand your thought logistics.

Here’s what the final result will look like without pseudocode.

function findFarmerAge(farmerTree, ageTest) {
var farmers = [];
function findFarmer(farmer) {
if (ageTest(farmer)) {
farmers.push(farmer.owner);
return farmers;
}
var reference = farmers.branch;
for (var i = 0; i < reference.length; i++) {
findFarmer(reference[i]);
}
}
findFarmer(farmerTree);
return farmers;

That will give us the array = [‘Farmer Jill’, ‘Farmer Pear’].

A final word on recursion, this is just one of many solutions that you can solve for this type of problem.

I hope you enjoyed my explanation on recursion, and please send me feedback, email, or just leave a comment. Cookies are acceptable too. :)