Hey guys I have a mission on my course that I need to build a function that given a tree (tree) is represented as a tuple returns a new tree (show of Tree)
class Tree(object):
def __init__(self, value, nodes=None):
self.value = value
self.nodes = nodes
def __repr__(self):
if self.nodes:
return 'Tree({0},{1})'.format(self.value, repr(self.nodes))
return 'Tree({0})'.format(self.value)
def BuildTree(tree):
if type(tree) != tuple:
return Tree(tree)
<3 >
return Tree("here i need to put the height" ,list(map(BuildTree, tree)))
t1 = BuildTree((((1, 2), 3), (4, (5, 6))))
print(t1)
and until now I get only like this.
Tree([Tree([Tree([Tree(1), Tree(2)]), Tree(3)]), Tree([Tree(4), Tree([Tree(5), Tree(6)])])])
But I don't know I calculate the height of each internal node
I have this pic that describes the construction of the new tree.
So the numbers in green are the height and the numbers in red are values I have inside the tuple
so the correct output of the new Tree is like that.
Tree(3,[Tree(2,[Tree(1,[Tree(1), Tree(2)]), Tree(3)]), Tree(2,[Tree(4), Tree(1,[Tree(5), Tree(6)])])])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…