I've made custom class for nodes
class NodeTree(object): def __init__(self, name = None, children = None): self.name = name self.children = children and defined a function that make a tree(a node containing its children nodes)
def create_tree(d): x = NodeTree() for a in d.keys(): if type(d[a]) == str: x.name = d[a] if type(d[a]) == list: if d[a] != []: for b in d[a]: x.add_child(create_tree(b)) return x The input is a dict with one argument for the node name and a list with its children in the same form as the parent. The function work fine and I've made method that prove it but I can't find a way to traverse it right and get the height of the tree. I don't know if "height" it's the right term cause I know it may be ambivalent, I need to count the node as a measure unit, like this:
parent | | --------- | | child child The height of this tree is 2, I've tried everything, from counters to tag in the class, everything seems to degenerate an I never get the right height. How should I approach that?