1

I am sorry i meet the akward problem in my python program. First of all I am newbie here and I am a Chinese student. Please forgive my poor English. I will try my best to express the problem clearly.

the Error Code is 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 I print the node.obj in find function it works well but when I print the obj in Find function, it return None.
This question is bothering me all day. if someone could solve it, it will be very appreciated.

thanks guy

Complete code here:i just write the 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 and 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()
4

2 回答 2

4

您的函数中缺少一些returns 。__find__将出现的两个 更改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

您再次调用该函数,但没有返回值。由于您没有显式返回值,None因此隐式返回。

于 2013-05-20T03:58:01.017 回答
1

您需要将代码更改为:

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:
        return node.obj
于 2013-05-20T04:00:24.833 回答