I have been trying to convert a list of lists format to a binary tree object which consists of nodes and references. So far, I have a 3 cases where the left and right node is empty, left is empty and right is empty. The problem is that whenever I test my function, the recursion does not work. By that I mean the recursion depth is only one level before it returns the converted binary tree.
argument = ['a', ['b', ['d', None, None],['f', None, None]], ['c', None, ['h', None, None]]]
def linkedlisttobinarytree (l):
'''
list of lists -> (nodes and references)
Takes a list of lists and returns a binary tree in nodes and references form
'''
bt = BinaryTree(l[0])
if (l[1] and l[2]) == None:
return bt.getRootValue()
elif l[1] != None and l[2] == None:
bt.left = ll2nr(l[1])
elif l[2] != None and l[1] == None:
bt.right = ll2nr(l[2])
return bt
For example, when I send the variable 'argument' into my method, it only yields 'a' as the root node and only 'a', the method only converts the first element in 'argument'. Can someone clarify why my the recursion depth is so shallow?