r我正在尝试以预购形式打印出我的二叉树,但是我遇到了这些错误。我还在学习python,所以我不太确定发生了什么。但我假设我的打印功能无法正常工作。不太确定为什么preorder_print会遇到全局名称问题=/
我的预期输出将是
pre order:
4
2
1
3
8
6
10
输出:
pre order:
<BST_tree.Node instance at 0x0000000002AA0988>
<BST_tree.Node instance at 0x0000000002AA0E08>
<BST_tree.Node instance at 0x0000000002AA0E88>
我的代码:
class Node:
def __init__(self,value):
self.right = None
self.left = None
self.value = value
def BST_Insert(root, node): # root --> root of tree or subtree!
if root.value is None:
root = node # beginning of tree
else:
if root.value > node.value: # go to left
if root.left is None:
root.left = node
else:
BST_Insert(root.left, node)
else:
if root.value < node.value: # go to right
root.right = node
else:
BST_Insert(root.right, node)
def preorder_print(root):
print root
if root.left is not None:
preorder_print(root.left)
else:
if root.right is not None:
preorder_print(root.right)
r = Node(4)
# left
a = Node(2)
b = Node(1)
c = Node(3)
# right
d = Node(8)
e = Node(6)
f = Node(10)
BST_Insert(r, a)
BST_Insert(r, b)
BST_Insert(r, c)
BST_Insert(r, d)
BST_Insert(r, e)
BST_Insert(r, f)
print "pre order:"
preorder_print(r)
*编辑*
谢谢大家,尤其是阿巴纳特的帮助!!这是固定版本!或preorder_print和BST_Inert
def BST_Insert(root, node): # root --> root of tree or subtree!
if root.value is None:
root = node # beginning of tree
else:
if root.value > node.value: # go to left
if root.left is None:
root.left = node
else:
BST_Insert(root.left, node)
if root.value < node.value: # go to right
if root.right is None:
root.right = node
else:
BST_Insert(root.right, node)
def preorder_print(root):
print root.value
if root.left is not None:
preorder_print(root.left)
if root.right is not None:
preorder_print(root.right)
很抱歉发布两个答案,但我不确定你在这里问的是两个问题中的哪一个。
您的预序遍历不会覆盖整个树,因为只要左侧子树不为空,您就会忽略整个右侧子树:
def preorder_print(root):
print root
if root.left is not None:
preoder_print(root.left)
else:
if root.right is not None:
preorder_print(root.right)
因此,在您的示例中,由于 4
节点的左侧有 2
节点,因此它不会查看8
或其下方的任何内容。然后在2
中也是如此.因此,您只能获得 3 个节点,而不是全部 7 个节点。
要解决此问题,只需删除else
:
def preorder_print(root):
print root
if root.left is not None:
preoder_print(root.left)
if root.right is not None:
preorder_print(root.right)
您的BST_Insert
功能也有问题。您可以随时设置root.right = node
node.value > root.value
,即使那里已经有东西了。因此,当您第一次尝试插入任何事物右侧左侧的内容时,它会擦除父项 - 6
擦除8
,然后10
擦除6
,所以你最终只有 4、2、1、3 和 10。
我认为您在这里想要的是改变这一点:
else:
if root.value < node.value: # go to right
root.right = node
else:
BST_Insert(root.right, node)
。自:
elif root.value < node.value: # go to right
if root.right is None
root.right = node
else:
BST_Insert(root.right, node)
打印功能工作正常。当您打印出没有自定义__repr__
或__str__
方法的对象时,这正是您应该获得的。
有两种方法可以解决这个问题。
首先,打印要打印
的信息,而不是打印Node
对象本身。例如,更改以下内容:
print root
。自:
print 'node with value {}'.format(root.value)
。或:
print root.value
。或:
print 'I've got a node and he's got a value and it's ' + str(root.value)
或者,如果您总是希望节点以相同的方式打印出来(例如,Node(4)
(,则可以为类提供一个__repr__
方法:
def __repr__(self):
return 'Node({})'.format(self.value)
有时,您希望提供一个很好的人类可读的类表示形式,您可以将其放入报表中,以及另一种有用的表示形式,例如,在交互式解释器上进行试验。在这种情况下,您可以定义__str__
和__repr__
:
def __str__(self):
# Pick whatever you think looks nice here
return str(self.value)
# return 'Node: ' + str(self.value)
# return 'Node with value {}'.format(self.value)
def __repr__(self):
return 'Node({})'.format(self.value)
(请注意,Node(4)
是一个很好的"交互式解释器实验"表示形式,因为它正是您在解释器中键入以创建等效对象的内容。
使用 print root.value
而不是 print root
。
解释: root
是一个对象,是Node
类的一个实例。 root.value
是节点持有的实际数字。
旁白:这样做的"正确"方法是@abarnert通过__repr__
回答的,但对于专注于树木教学的简单练习来说,这有点矫枉过正。
你想打印 root 的值
print root.value