Home > Mobile >  Counting the highest level of nested Arrays inside Object
Counting the highest level of nested Arrays inside Object

Time:09-12

I have a problem with a task, I have to count the highest level in a structure which looks like this:

let node = {
    age: 23,
    name: "christian",
    children: [{
        age: 25,
        name: "michael",
        children: [{
            age: 33,
            name: "Johann",
            children: [{
                age: 45,
                name: "Christiaaann",
            }]
        }]
    }, {
        age: 90,
        name: "Monika",
        children: [{
            age: 10,
            name: "WHATEVER",
        }]
    }]
};

The level of the first subtree would be 3 as it contains 3 nested children arrays. The right level would be 2 as it contains 2 nested children arrays.

I tried to solve it recursively, and of course I know that I have to count the level of each subtree and then check it with a condition if it is greater than the previous maximum.

My problem is that I don't know where to count it up in the recursive call.

This is my solution, so far

let node = {
    age: 23,
    name: "christian",
    children: [{
        age: 25,
        name: "michael",
        children: [{
            age: 33,
            name: "Johann",
            children: [{
                age: 45,
                name: "Christiaaann",
            }]
        }]
    }, {
        age: 90,
        name: "Monika",
        children: [{
            age: 10,
            name: "WHATEVER",
        }]
    }]

};

let level_count;
let max_count = 0;

function level_counter(obj, func) {
    level_count = 0;
    func(obj);
    console.log("INSIDEFUNCTION", level_count);


    if (obj.children) {
        level_count  ;
        obj.children.forEach(function(child) {
            console.log(child);
            level_counter(child, func);


        });
        if (level_count > max_count) {
            max_count = level_count;
        }
    }
}

function tree_get_levels(root) {
    level_counter(root, function(obj) { });
    console.log("DOWNONE", level_count);
    return 0;
}

let result = tree_get_levels(node);
console.log(result);

CodePudding user response:

I tried to solve it recursively, and of course I know that I have to count the level of each subtree and then check it with a condition if it is greater than the previous maximum.

My problem is that I don't know where to count it up in the recursive call.

You need to use the return value of the recursive call, adding one for the level making the call to it. Separately, always avoid global variables when you're trying to write a recursive solution; every level of recursion sees the same values for those variables, and assigning to them won't work correctly. Stick to variables defined within the recursive function, and (again) report values back up the stack by returning them.

See inline notes in the code:

let node = {
    age: 23,
    name: "christian",
    children: [{
        age: 25,
        name: "michael",
        children: [{
            age: 33,
            name: "Johann",
            children: [{
                age: 45,
                name: "Christiaaann",
            }]
        }]
    }, {
        age: 90,
        name: "Monika",
        children: [{
            age: 10,
            name: "WHATEVER",
        }]
    }]
};

function countLevels(obj, func) {
    // No levels so far
    let levelCount = 0;
    func(obj); // Not sure what this function is for

    // If there are any children...
    if (obj.children) {
        // There are, so we've already gone one level down
        for (const child of obj.children) {
            // Get levels starting at this child, then add one because we've
            // already gone one level down
            const levelsFromChild = countLevels(child, func)   1;
            // Remember it if it's higher than the maximum we've seen
            if (levelCount < levelsFromChild) {
                levelCount = levelsFromChild;
            }
            // Or that could be replaced with:
            // levelCount = Math.max(levelCount, countLevels(child, func)   1);
        }
    }
    // I added this so you could see the levels count starting from each object
    console.log(`${levelCount} level(s) from ${JSON.stringify(obj)}`);
    // Return the levels found from this object
    return levelCount;
}

// No need for a separate wrapper function, just call the counter directly
let result = countLevels(node, function func(obj) { });
console.log(`Deepest: ${result}`);
.as-console-wrapper {
    max-height: 100% !important;
}

CodePudding user response:

You can try this:


function highestDepth(obj) {
  let depth = 0;

  if (Array.isArray(obj.children) && obj.children.length) {
    depth  = 1;

    let subDepth = 0;
    for (const child of obj.children) {
      subDepth = Math.max(subDepth, highestDepth(child));
    }

    depth  = subDepth;
  }

  return depth;
}

console.log(highestDepth(node));
  • Related