Python 的二叉树,getRoot 没有 return 根
Binary Tree for Python, getRoot does not return root
我一直在查看这个二叉树代码以了解它的作用:
class BinaryTree:
def __init__(self):
self.root = None
self.size = 0
# Insert element e into the binary search tree
# Return True if the element is inserted successfully
def insert(self, e):
if self.root == None:
self.root = self.createNewNode(e) # Create a new root
else:
# Locate the parent node
parent = None
current = self.root
while current != None:
if e < current.element:
parent = current
current = current.left
elif e > current.element:
parent = current
current = current.right
else:
return False # Duplicate node not inserted
# Create the new node and attach it to the parent node
if e < parent.element:
parent.left = self.createNewNode(e)
else:
parent.right = self.createNewNode(e)
self.size += 1 # Increase tree size
return True # Element inserted
# Create a new TreeNode for element e
def createNewNode(self, e):
return TreeNode(e)
# Return the size of the tree
def getSize(self):
return self.size
# Return true if the tree is empty
def isEmpty(self):
return self.size == 0
# Remove all elements from the tree
def clear(self):
self.root == None
self.size == 0
# Return the root of the tree
def getRoot(self):
return self.root
class TreeNode:
def __init__(self, e):
self.element = e
self.left = None # Point to the left node, default None
self.right = None # Point to the right node, default None
如果我执行以下代码:
tree = BinaryTree()
tree.insert("500")
print(tree.getRoot)
print(tree.getRoot())
结果:
<bound method BinaryTree.getRoot of <__main__.BinaryTree object at 0x036E1E68>>
<__main__.TreeNode object at 0x036FA1F0>
即使它是根,我也不会取回字符串“500”。 getRoot 函数中是否缺少某些内容?我找不到导致函数无法 return 字符串的原因。
tree.getRoot
是 BinaryTree
对象的一个方法。
tree.getRoot()
正在调用该方法,它 returns 是一个 TreeNode
对象。如果你想得到“500”,你应该得到它的 element
属性。
所以你可以试试下面的代码:
tree = BinaryTree()
tree.insert("500")
print(tree.getRoot().element)
打印
500
我一直在查看这个二叉树代码以了解它的作用:
class BinaryTree:
def __init__(self):
self.root = None
self.size = 0
# Insert element e into the binary search tree
# Return True if the element is inserted successfully
def insert(self, e):
if self.root == None:
self.root = self.createNewNode(e) # Create a new root
else:
# Locate the parent node
parent = None
current = self.root
while current != None:
if e < current.element:
parent = current
current = current.left
elif e > current.element:
parent = current
current = current.right
else:
return False # Duplicate node not inserted
# Create the new node and attach it to the parent node
if e < parent.element:
parent.left = self.createNewNode(e)
else:
parent.right = self.createNewNode(e)
self.size += 1 # Increase tree size
return True # Element inserted
# Create a new TreeNode for element e
def createNewNode(self, e):
return TreeNode(e)
# Return the size of the tree
def getSize(self):
return self.size
# Return true if the tree is empty
def isEmpty(self):
return self.size == 0
# Remove all elements from the tree
def clear(self):
self.root == None
self.size == 0
# Return the root of the tree
def getRoot(self):
return self.root
class TreeNode:
def __init__(self, e):
self.element = e
self.left = None # Point to the left node, default None
self.right = None # Point to the right node, default None
如果我执行以下代码:
tree = BinaryTree()
tree.insert("500")
print(tree.getRoot)
print(tree.getRoot())
结果:
<bound method BinaryTree.getRoot of <__main__.BinaryTree object at 0x036E1E68>>
<__main__.TreeNode object at 0x036FA1F0>
即使它是根,我也不会取回字符串“500”。 getRoot 函数中是否缺少某些内容?我找不到导致函数无法 return 字符串的原因。
tree.getRoot
是 BinaryTree
对象的一个方法。
tree.getRoot()
正在调用该方法,它 returns 是一个 TreeNode
对象。如果你想得到“500”,你应该得到它的 element
属性。
所以你可以试试下面的代码:
tree = BinaryTree()
tree.insert("500")
print(tree.getRoot().element)
打印
500