0
我正在嘗試打印出樹節點,但是所有它出來的都是最低深度的noes!我不確定我的邏輯有什麼問題,所以我想知道如果另一組眼睛會幫助我或給我一個提示,謝謝!打印BST IN訂單
預期輸出:
1
2
3
4
6
8
10
我的代碼:
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)
if root.value < node.value: # go to right
if root.right is None:
root.right = node
else:
BST_Insert(root.right, node)
def inorder_print(root):
if root.left is not None:
inorder_print(root.left)
else:
print root.value
if root.right is not None:
inorder_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 "in order:"
inorder_print(r)
thanks!那就是訣竅! – Liondancer