我正在制作一种方法来查找二叉树中的最大值,我认为我做对了,但是当我在其上运行我的文档测试时,它说的是预期值,但然后说“什么也没有”。我不确定这意味着什么或为什么会发生。
def best_apartment(self) -> None:
"""Return (one of) the apartment(s) with the best evaluation
score in the BSTTree.
>>> apartments = read_apartment_data("apartments.csv")[0:300]
>>> bst = BSTTree()
>>> bst.build_tree(apartments)
>>> bst.best_apartment()
PRIVATE: 80
"""
if self.is_empty():
return None
current_node = self
while current_node.right is not None:
current_node = current_node.right
return current_node.apartment
这是我的方法代码
“Got Nothing”表示没有输出。
在这种情况下,这是因为在交互模式下,Python 不显示
None
值。例如:
>>> None # No output
>>>
>>> lst = []
>>> lst.append(1) # This function returns None, so there's no output
>>> lst
[1]
您只需将文档测试中的代码复制到交互式会话中并运行它即可亲自确认这一点。
所以,问题是你的函数正在返回
None
。它在哪里做到这一点? if self.is_empty():
。所以根本问题不在您此处显示的代码中。