一棵json树的深度

时间:2015-03-12 09:27:12

标签: python json depth

我有一个形式的树:

{  
    "id":442500000904671234,
    "reply":0,
    "children":[  
        {  
            "id":442500532536893440,
            "reply":1,
            "children":[  
                {  
                    "id":442500826561785856,
                    "reply":1,
                    "children":[  
                        {  
                            "id":442501277688545280,
                            "reply":1,
                            "children":[  
                                {  
                                    "id":442501561940709376,
                                    "reply":1,
                                    "children":[  
                                        {  
                                            "id":442501884709199872,
                                            "reply":1,
                                            "children":[  

                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {  
            "id":442500099315597312,
            "reply":0,
            "children":[  

            ]
        }
    ]
}

如何在python中找到树的最大深度?

5 个答案:

答案 0 :(得分:2)

您可以使用python的json库解析JSON,然后使用递归函数计算深度。

import json

def depth(jsn):
    if jsn.has_key('children'):
        return 1 +  max([0] + map(depth, jsn['children']))
    else:
        return 1

j = json.load(file('your_file.json'))
print depth(j)

答案 1 :(得分:0)

你可以使用一个堆栈推送{然后当你遇到一个}然后弹出之前检查你当前的深度是否大于你到目前为止的最大深度,即如果stack.count大于已达到的最大数量。

我会在使用代码示例

后更新我的答案

答案 2 :(得分:0)

如何将json加载到dict并遍历树。至少应该提出一些进一步的想法。

import json

def depth(root):
    if root is None:
        return 0
    if isinstance(root, list):
        return max(map(depth, root)) + 1
    if not isinstance(root, dict):
        return 1
    return max(depth(v) for k, v in root.items()) + 1

print depth(json.load(file('your_data.json')))

答案 3 :(得分:0)

我认为你可以使用DFS或BFS走树,然后计算深度:

#load in your data
json_tree = json.loads(your_data)

depth = 0

if json_tree:
    depth += 1

#add a root node + it's depth
to_visit = [(json_tree, depth)] 

max_depth = 0

while to_visit:
    #get first from the list
    current = to_visit.pop(0)

    #if depth greater than the current max update max 
    if current[1] > max_depth:
        max_depth = current[1]

    # append all children to the list with increased depth
    for child in current[0]['children']:
        to_visit.append((child, current[1] + 1))


print(max_depth)
这样做的好处是你实际上是在检查数据结构,而不是依赖于字符串表示

答案 4 :(得分:0)

将json转换为dict,

count_global = 0


def find_depth(tree_dict=None, count=0):

    if not tree_dict['children']:
        global count_global
        if count_global < count:
            count_global = count

    for child in tree_dict['children']:
        count = count + 1
        find_depth(child, count)