Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the depth of a tree data structure in a simpler way

I have a JSON-like hierarchy of JS objects in the following format:

[
  {
    subs: [ ...other objects... ]
  },
  ...other objects...
]

I wrote a method that returns the number of levels of such a hierarchy:

/* Returns the depth of the tree. */
public getDepth(): number {

  function f(obj: object): number {
    let depth = 0;
    if (obj['subs'].length > 0) {
      obj['subs'].forEach((s: object) => {
        const tempDepth = f(s);
        if (tempDepth > depth) depth = tempDepth;
      });
    }
    return depth + 1;
  }

  if (this.tree.length > 0)
    return Math.max(...this.tree.map((s: object) => f(s)));
  else return 0;

}

It works but it's too complicated. Then, I've found this, much cleaner code: https://stackoverflow.com/a/16075976/5214911

The only difference is that I have not one base object but an array of objects as root. How could I simplify the code to spare that extra if and iteration?

Example data:

const data1 = []; // depth: 0

const data2 = [{}, {}, {}]; // depth: 1

const data3 = [{}, // depth: 5
  {
    "subs": [{
      "subs": [{
        "subs": [{}]
      }, {
        "subs": [{
          "subs": [{}]
        }]
      }]
    }, {
      "subs": [{
        "subs": [{}]
      }]
    }]
  },
  {}
];
like image 264
tom Avatar asked Oct 30 '25 06:10

tom


1 Answers

You could map the depth of every children and take the maximum value of it.

function getDepth(array) {
    return 1 + Math.max(0, ...array.map(({ subs = [] }) => getDepth(subs)));
}

const
    data1 = [],
    data2 = [{}, {}, {}],
    data3 = [{}, { subs: [{ subs: [{ subs: [{}] }, { subs: [{ subs: [{}] }] }] }, { subs: [{ subs: [{}] }] }] }, {}];

console.log(getDepth(data1) - 1); // 0
console.log(getDepth(data2) - 1); // 1
console.log(getDepth(data3) - 1); // 5
like image 119
Nina Scholz Avatar answered Oct 31 '25 19:10

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!