class Node(object):
def __init__(self,value):
self.value=value
self.left=None
self.right=None
class Tree(object):
def __init__(self,values):
length=len(values)
node=[]
for x in range(length):
node.append(Node(values[x]))
for x in range(length):
if 2*x+1 < length:
node[x].left = node[2*x+1]
if 2*x+2 < length:
node[x].right = node[2*x+2]
if node[0] != None:
self.root = node[0]
else:
self.root = None
def DFS(self,node,obj):
if node == None or node.value == obj:
return node
else:
self.DFS(node.left,obj)
self.DFS(node.right,obj)
def main():
obj = raw_input("Please input a number you want to search: ")
tree = Tree(range(100))
node = tree.DFS(tree.root,obj)
if node.value == obj:
print "I've done it!"
if __name__ == "__main__":
main()
嗨,我是python和这个平台的新手。我想在我的python&#39; Tree&#39;中实现DFS。但是shell说“树”#39;对象没有属性&#39; DFS&#39;。但是,DFS在我的代码中明确定义。谁能帮我这个?非常感谢提前!
答案 0 :(得分:1)
执行您的代码,我收到一条不同的错误消息:
if node.value == obj:
AttributeError: 'NoneType' object has no attribute 'value'
此错误消息是因为Tree.DFS()
实际上并不总是显式返回任何内容(因此会返回None
),并且None
没有value
属性
答案 1 :(得分:1)
我无法看到您说的问题。我从这里得到一个错误:
def DFS(self,node,obj):
if node == None or node.value == obj:
return node
AttributeError: 'NoneType' object has no attribute 'value'
当node == None
返回None
时,value
不能/不具有$http.get('json.php').then(function(response){
// Place a debugger statement here to see if it's successful
$scope.myData = response.data.bands;
}, function(error) {
// No breakpoint needed as you'll see the error in the console window
console.error(error);
});
属性。