Search code examples
javascripttreedepth

i wanna get the max depth of a not-certain tree. the tree looks like below,how can i finish it


given a tree-structured data, get the max height of the tree. i wanna get the max depth of a not-certain tree. the tree looks like below:

{
      id: 1,
      label: 'label1',
      children: [{
        id: 3,
        label: 'label2',
        children: [{
          id: 4,
          label: 'label3'
        }, {
          id: 5,
          label: 'label4',
          disabled: true,
          children: [{
              id: 4,
              label: 'label3'
            }, {
              id: 5,
              label: 'label4',
              disabled: true
            }]
        }]
      }

i tried as below, but it did not work as expected.

const maxDepth = o => {

  if(!o || !o.children) return 0;

  let arr = []

  for(let i = 0; i< o.children.length; i++) {
    arr[i] = maxDepth(o.children[i])
  }

  let max = Math.max(...[arr]) + 1
  return max
}

Solution

  • I don't believe your data is formatted 100% correctly, so I took the liberty of doing so. That being said, this screams for a recursive algorithm.

    {
      id: 1,
      label: 'label1',
      children: [{
        id: 3,
        label: 'label2',
        children: [{
          id: 4,
          label: 'label3'
        },
        {
          id: 5,
          label: 'label4',
          disabled: true,
          children: [{
            id: 4,
            label: 'label3'
          },
          {
            id: 5,
            label: 'label4',
            disabled: true
          }]
        }]
      }]
    }
    

    test1 = {
      id: 1,
      label: "test1",
      children: []
    }
    
    test2 = {
      id: 2,
      label: "test1",
      children: [
      {
        id: 2,
        label: "test2",
        children: []
      },
      {
        id: 2,
        label: "test2",
        children: []
      }]
    }
    
    test3 = {
      id: 3,
      label: "test1",
      children: [
      {
        id: 3,
        label: "test2",
        children: [{
          children: [{
            children: [{}]
          }]
        }]
      },
      {
        id: 3,
        label: "test2",
        children: [{}]
      }]
    }
    
    your_data = {
      id: 1,
      label: 'label1',
      children: [{
        id: 3,
        label: 'label2',
        children: [{
          id: 4,
          label: 'label3'
        },
        {
          id: 5,
          label: 'label4',
          disabled: true,
          children: [{
            id: 4,
            label: 'label3'
          },
          {
            id: 5,
            label: 'label4',
            disabled: true
          }]
        }]
      }]
    }
    
    my_data = {
      id: 1,
      label: 'label1',
      children: [{
        id: 3,
        label: 'label2',
        children: [{
          id: 4,
          label: 'label3'
        },
        {
          id: 5,
          label: 'label4',
          disabled: true,
          children: [{
            id: 4,
            label: 'label3'
          },
          {
            id: 5,
            label: 'label4',
            disabled: true
          }]
        }]
      },
      {
        id: 6,
        label: 'madeup1',
        children: [{
          id: 7,
          label: 'madeup2',
          children: [{
            id: 8,
            label: 'madeup3',
            children: [{
              id: 9,
              label: 'madeup4'
            }]
          }]
        }]
      }]
    }
    
    function max_depth(exploringTheDepthsOf)
    {
        largest = 0;
        if (exploringTheDepthsOf.hasOwnProperty('children'))
        {
            for (var i = 0; i < exploringTheDepthsOf["children"].length; i++)
            {
                largest = Math.max(largest, max_depth(exploringTheDepthsOf["children"][i]));
            }
        }
        else
        {
            return 0;
        }
        return largest + 1;
    }
    
    console.log("returned value", max_depth(test1));
    console.log("returned value", max_depth(test2));
    console.log("returned value", max_depth(test3));
    console.log("returned value", max_depth(your_data));
    console.log("returned value", max_depth(my_data));

    This is about as close as I could get. Geeks for geeks has a pretty good artical on it and javascript code to show you how to do it, but its for actual nodes, not for json like objects: https://www.geeksforgeeks.org/depth-n-ary-tree/