I get the following tuple :(12, (6, (2,), (8,)), (15, (14,), (18,))) and I need to return an instance of Tree in the Following form : Tree(12, Tree(6, Tree(2), Tree(8)), Tree(15, Tree(14), Tree(18)))
My class is :
class Tree():
def __init__(self, entry, left=None, right=None):
self.entry = entry
self.left = left
self.right = right
def __repr__(self):
if not self.left and not self.right:
return "Tree({0})".format(repr(self.entry))
return "Tree({0},{1},{2})".format(repr(self.entry),repr(self.left),repr(self.right))
The problem that I can not understand is why it does not enter to the base condition? And how can I fix the function.
The function I did is:
def build_tree(tree):
if type(tree) != tuple:
return Tree(tree)
return Tree(tree[0], build_tree(tree[1]), build_tree(tree[2]))
The activation is :
tree1 = (12, (6, (2,), (8,)), (15, (14,), (18,)))
t1=build_tree(tree1)
print(t1)