How to reduce code in tree node count function

Viewed 18

In my use case I need to create a function that counts the records of an array tree.

Currently I can count the records using the countRecords function as you can see in the code below:

const data = [
  {
node: 7,
name: 'Step1 - record 1',
children: [
  {
    node: 3,
    name: 'Step2 - record 1',
    children: [
      {
        node: 2,
        name: 'Step3 - record 1',
        children: [
          {
            node: 1,
            name: 'Step4 - record 1',
          },
        ],
      },
    ],
  },
  {
    node: 6,
    name: 'Step2 - record 2',
    children: [
      {
        node: 5,
        name: 'Step3 - record 2',
        children: [
          {
            node: 4,
            name: 'Step4 - record 2',
          },
        ],
      },
    ],
  },
],
  },
  {
node: 11,
name: 'Step1 - record 2',
children: [
  {
    node: 10,
    name: 'Step2 - record 3',
    children: [
      {
        node: 9,
        name: 'Step3 - record 3',
        children: [
          {
            node: 8,
            name: 'Step4 - record 3',
          },
        ],
      },
    ],
  },
],
  },
];

const countRecords = array => {
  let sum = array.reduce(function (countStep1, step1) {
countStep1 = countStep1 + 1;

if (step1.children.length > 0) {
  let sumStep2 = step1.children.reduce(function (countStep2, step2) {
    countStep2 = countStep2 + 1;
    if (step2.children.length > 0) {
      let sumStep3 = step2.children.reduce(function (countStep3, step3) {
        countStep3 = countStep3 + 1;
        if (step3.children.length > 0) {
          let sumStep4 = step3.children.reduce(function (countStep4) {
            countStep4 = countStep4 + 1;
            return countStep4;
          }, 0);
          countStep3 = countStep3 + sumStep4;
        }
        return countStep3;
      }, 0);
      countStep2 = countStep2 + sumStep3;
    }
    return countStep2;
  }, 0);
  countStep1 = countStep1 + sumStep2;
}
return countStep1;
  }, 0);
  return sum;
};

console.log("Total records in the tree: ",countRecords(data));

The countRecords function is working, but is there any way to improve and reduce this code?

I thank you for your help!

1 Answers

As pointed out in the comments section, using a recursive function is very clean and also allows you to handle an arbitrary amount of nested children.

In this example I added a new deep-nested child, result is now 12.

const data = [{
    node: 7,
    name: 'Step1 - record 1',
    children: [{
        node: 3,
        name: 'Step2 - record 1',
        children: [{
          node: 2,
          name: 'Step3 - record 1',
          children: [{
            node: 1,
            name: 'Step4 - record 1',
            children: [{
              node: 0,
              name: 'NEW! Step5 - record 1',
            }, ],
          }, ],
        }, ],
      },
      {
        node: 6,
        name: 'Step2 - record 2',
        children: [{
          node: 5,
          name: 'Step3 - record 2',
          children: [{
            node: 4,
            name: 'Step4 - record 2',
          }, ],
        }, ],
      },
    ],
  },
  {
    node: 11,
    name: 'Step1 - record 2',
    children: [{
      node: 10,
      name: 'Step2 - record 3',
      children: [{
        node: 9,
        name: 'Step3 - record 3',
        children: [{
          node: 8,
          name: 'Step4 - record 3',
        }, ],
      }, ],
    }, ],
  },
];


let total = 0

for (const root of data) {
  total++
  if (root.hasOwnProperty('children')) {
    recursiveFunct(root.children)
  }
}

console.log("Total records in the tree: ", total)


function recursiveFunct(parent) {
  for (const child of parent) {
    total++
    if (child.hasOwnProperty('children')) {
      // RECURSIVE CALL
      recursiveFunct(child.children)
    }
  }
}

Related