Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 比较BST节点与str问题_Python_String_Binary Search Tree_Nodes_Typeerror - Fatal编程技术网

Python 比较BST节点与str问题

Python 比较BST节点与str问题,python,string,binary-search-tree,nodes,typeerror,Python,String,Binary Search Tree,Nodes,Typeerror,这是我正在使用的搜索函数,用于尝试查找BST中包含单词的特定值。但是,当我运行它时,在elif root

这是我正在使用的搜索函数,用于尝试查找BST中包含单词的特定值。但是,当我运行它时,在
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()
),或从节点提取字符串并比较它们。