【发布时间】:2020-04-21 17:03:07
【问题描述】:
为什么这个插入二叉搜索树的代码不起作用?
class BinaryTreeNode:
def __init__(self,key):
self.key=key
self.left=None
self.right=None
def insert(root,data):
if root is None:
root=BinaryTreeNode(data)
else:
if data>root.key:
insert(root.right,data)
else:
insert(root.left,data)y
【问题讨论】:
-
看起来
else:块中的代码不应该在else块内 -
欢迎来到 SO!请澄清究竟是什么不起作用。
-
你永远不会给
root.left或root.right分配任何东西。 -
太多的拼写错误:删除最后一行的
y,第二个defies not properly indented,insert`需要有一个self参数,如果它是一个方法并且你从不分配左右。 -
@MarkMeyer 如果说 root.right 是 None 那么它将被分配递归调用中的节点对吗?
标签: python data-structures binary-search-tree