Python 比较BST节点与str问题
这是我正在使用的搜索函数,用于尝试查找BST中包含单词的特定值。但是,当我运行它时,在Python 比较BST节点与str问题,python,string,binary-search-tree,nodes,typeerror,Python,String,Binary Search Tree,Nodes,Typeerror,这是我正在使用的搜索函数,用于尝试查找BST中包含单词的特定值。但是,当我运行它时,在elif root
elif root
def search(root, target):
print("Target is: " + target + "\n")
if root.value == target or root == None:
print("Found target: " + target)
return root
elif root < root.value:
return search(root.left, target)
print("Searching left")
else:
return search(root.right, target)
print("Searching right")
def搜索(根,目标):
打印(“目标为:“+Target+”\n”)
如果root.value==目标或root==无:
打印(“找到目标:+目标”)
返回根
elif root
它说
TypeError:“您正试图将root
与root.value
进行比较
我想你的意思是这样的:
def search(root, target):
print("Target is: " + target + "\n")
if root.value == target or root is None: # changed to is,
# though you may want a 4th case for this
print("Found target: " + target)
return root
elif target < root.value: #Notice I changed this to target instead of root
print("Searching left")
return search(root.left, target)
else:
print("Searching right")
return search(root.right, target)
def搜索(根,目标):
打印(“目标为:“+Target+”\n”)
如果root.value==target或root为None:#更改为is,
#虽然你可能需要第四箱
打印(“找到目标:+目标”)
返回根
elif target
为节点定义比较(使用def\uu lt\uu()
),或从节点提取字符串并比较它们。