当我尝试编译时,它给了我这个错误,我认为因为我使用的最大功能,有人可以帮我解决这个问题吗? class BinaryTree: def init (个体经营): self.root =无 self.size = 0
# Return True if the element is in the tree
def search(self, e):
current = self.root # Start from the root
while current != None:
if e < current.element:
current = current.left
elif e > current.element:
current = current.right
else: # element matches current.element
return True # Element is found
return False
# 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)
def leaf(self):
self.leafHelper(self.root)
def leafHelper(self,r):
if r == None:
return r
else:
self.leafHelper(r.left)
if(r.left == None and r.right == None):
print(r.element, end = " ")
self.leafHelper(r.right)
def nonleaf(self):
self.nonleafHelper(self.root)
def nonleafHelper(self,r):
if r == None:
return r
else:
self.nonleafHelper(r.left)
if(r.left != None or r.right != None):
print(r.element, end = " ")
self.nonleafHelper(r.right)
def getHeight(self):
self.height(self.root)
def height(self,r):
if r == None:
return r
else:
return 1 + max(self.height(r.left),self.height(r.right))
"""
# Return the size of the tree
def getSize(self):
return self.size"""
# Inorder traversal from the root
def inorder(self):
self.inorderHelper(self.root)
# Inorder traversal from a subtree
def inorderHelper(self, r):
if r != None:
self.inorderHelper(r.left)
print(r.element, end = " ")
self.inorderHelper(r.right)
# Postorder traversal from the root
def postorder(self):
self.postorderHelper(self.root)
# Postorder traversal from a subtree
def postorderHelper(self, root):
if root != None:
self.postorderHelper(root.left)
self.postorderHelper(root.right)
print(root.element, end = " ")
# Preorder traversal from the root
def preorder(self):
self.preorderHelper(self.root)
# Preorder traversal from a subtree
def preorderHelper(self, root):
if root != None:
print(root.element, end = " ")
self.preorderHelper(root.left)
self.preorderHelper(root.right)
# 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
####################### Main test binary tree
def main(size = 7):
tree = BinaryTree()
tree.insert("George")
tree.insert("Michael")
tree.insert("Tom")
tree.insert("Adam")
tree.insert("Jones")
tree.insert("Peter")
tree.insert("Daniel")
# Traverse tree
print("Inorder (sorted): ", end = "")
tree.inorder()
print("\nPostorder: ", end = "")
tree.postorder()
print("\nPreorder: ", end = "")
tree.preorder()
print("\nLeaf: ", end = "")
tree.leaf()
print("\nNonleaf: ", end = "")
tree.nonleaf()
tree.getHeight()
numbers =[49, 76, 67, 29, 75, 18, 4, 83, 87, 40]
print ("\n\nInserting the following values:")
for i in numbers:
print(i, end=" ")
print()
intTree = BinaryTree()
for e in numbers:
intTree.insert(e)
print("\nPreorder traversal:")
intTree.preorder()
print("\n\nInorder traversal:")
intTree.inorder()
print("\n\nPostorder traversal:")
intTree.postorder()
print("\nLeaf: ", end = "")
intTree.leaf()
print("\nNonleaf: ", end = "")
intTree.nonleaf()
如果名称 ==“主要”: main()
答案 0 :(得分:1)
更改height()
功能,如下所示 -
def height(self,r):
if not r:
return 0
else:
return 1 + max(self.height(r.left),self.height(r.right))
您无法将NoneType
添加到Integer
。