python return value meet the error.help me -
i sorry meet akward problem in python program. first of newbie here , chinese student. please forgive poor english. try best express problem clearly.
the error code here:
def __find__(self,node,value): if value<node.v: self.__find__(node.l,value) elif value>node.v: self.__find__(node.r,value) elif value==node.v: return node.obj def find(self,value): obj=self.__find__(self.r,value) print obj return obj
when print node.obj in find function works when print obj in find function, return none.
question bothering me day. if solve it, appreciated.
thanks guy
complete code here:i write bts tree:
####class#### class bsttreenode: def __init__(self,lnode=none,rnode=none,parent=none,value=none,obj=none): self.l=lnode self.r=rnode self.p=parent self.v=value self.obj=obj class bstree: def __init__(self,rootnode=none): self.r=rootnode def insert(self,value,obj): self.__insert__(self.r,value,obj) def __insert__(self,node,value,obj): if value<=node.v: if node.l==none: localnode=bsttreenode(lnode=none,rnode=none,parent=node,value=value,obj=obj) node.l=localnode elif node.l!=none: self.__insert__(node.l,value,obj) if value>node.v: if node.r==none: localnode=bsttreenode(lnode=none,rnode=none,parent=node,value=value,obj=obj) node.r=localnode elif node.r!=none: self.__insert__(node.r,value,obj) def __find__(self,node,value): if value<node.v: self.__find__(node.l,value) elif value>node.v: self.__find__(node.r,value) elif value==node.v: return node.obj def find(self,value): obj=self.__find__(self.r,value) print obj return obj def print(self): self.__printtree__(self.r) def __printtree__(self,node): if node.l==none , node.r==none: print node.v,node.obj return elif node.r != none: self.__printtree__(node.r) elif node.l !=none: self.__printtree__(node.l) print node.v,node.obj def main(): mnode=bsttreenode(value=10,obj="first") bstree=bstree(rootnode=mnode) bstree.insert(value=20,obj="second") bstree.insert(30,obj="second") bstree.print() bstree.find(value=20) if __name__ =="__main__": main()
you missing return
s in __find__
function. change both occurrences of self.__find__(...)
return self.__find__(...)
.
def __find__(self, node, value): if value < node.v: return self.__find__(node.l, value) elif value > node.v: return self.__find__(node.r, value) elif value == node.v: print node.obj return node.obj def find(self, value): obj = self.__find__(self.r, value) print obj return obj
you call function again, don't return value. don't explicitly return value, none
implicitly returned.
Comments
Post a Comment