Interactivepython上的 Tree 插入函数不正确吗?
向左插入:
def insertLeft(root,newBranch):
t = root.pop(1)
if len(t) > 1:
root.insert(1,[newBranch,t,[]])
else:
root.insert(1,[newBranch, [], []])
return root
我发现逻辑不正确,插入导致树损坏。
我尝试了以下(您可以在同一页面上运行代码)并查看验证。
r = BinaryTree(3)
insertLeft(r,4)
insertLeft(r,5)
insertLeft(r, [10, [11, [],[]], []])
insertRight(r,6)
insertRight(r,7)
print(r)
输出:
[3,
[
[10, [11, [], []], []],
[5, [4, [], []], []],
[]
],
[
7,
[],
[6, [], []]
]
]