我得到了二进制搜索树的作业递归版本,我必须将数字从数组插入到BST,然后搜索它,查找数字是否存在(True)或不存在(False)。
class Node:
def __init__(self, value, left, right):
self.left = left
self.right = right
self.data = value
class BinarySearchTree:
def __init__(self):
self.root = Node(None, None, None)
def insert(self, value):
root = self.root
new_node = Node(value, None, None)
if root.data is None:
root = new_node
if new_node.data < root.data:
if root.left is None:
root.left = new_node
else:
root.left.insert(value)
root = root.left
else:
if root.right is None:
root.right = new_node
else:
root.right.insert(value)
root = root.right
def fromArray(self, array):
for i in range(len(array)-1):
value = array[i]
BinarySearchTree.insert(self, value)
i += 1
return BinarySearchTree
def search(self, value):
if (self.root is not None):
return self._search(value, self.root)
else:
return None
def _search(self, value, node):
new_node = Node(value, None, None)
if (new_node.data == node.data):
return True
elif (new_node.data < node.data and node.left is not None):
self._search(value, node.left)
elif (new_node.data > node.data and node.right is not None):
self._search(value, node.right)
else:
return False
def min(self):
root = self.root
if root.left is None:
return root
else:
self.min(root.left)
def max(self):
root = self.root
if root.right is None:
return root
else:
self.max(root.right)
即使我正在比较两个节点它写错误:
unorderable类型:int()&lt; NoneType()
出了什么问题?