Aren't the Tree insert functions on interactivepython incorrect?
Insert Left:
def insertLeft(root,newBranch):
t = root.pop(1)
if len(t) > 1:
root.insert(1,[newBranch,t,[]])
else:
root.insert(1,[newBranch, [], []])
return root
I found the logic to be incorrect, insertion is leading to a broken tree.
I tried the below (you can run the code on the same page) and see a validation.
r = BinaryTree(3)
insertLeft(r,4)
insertLeft(r,5)
insertLeft(r, [10, [11, [],[]], []])
insertRight(r,6)
insertRight(r,7)
print(r)
Output:
[3,
[
[10, [11, [], []], []],
[5, [4, [], []], []],
[]
],
[
7,
[],
[6, [], []]
]
]