如何从python中的递归函数返回值? [英] How to return value from recursive function in python?

查看:241
本文介绍了如何从python中的递归函数返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python中的二叉树.我需要创建一个搜索树并返回可以插入新值的最佳节点的方法.但是我很难从这个递归函数中返回一个值.我是python的新手.

I'm working with binary tree in python. I need to create a method which searches the tree and return the best node where a new value can be inserted. But i'm have trouble returning a value from this recursive function. I'm a total newbie in python.

def return_key(self, val, node):
    if(val < node.v):
        if(node.l != None):
            self.return_key(val, node.l)
        else:
            print node.v
            return node
    else:
        if(node.r != None):
            #print node.v
            self.return_key(val, node.r)
        else:
            print node.v
            return node

打印node.v会打印节点值,但是当我打印返回的节点时:

Printing node.v prints the node value, but when i print the returned node :

print ((tree.return_key(6, tree.getRoot().v)))

它打印

没有

结果.

推荐答案

您需要返回 recursive 调用的结果.您在这里忽略它:

You need to return the result of your recursive call. You are ignoring it here:

if(node.l != None):
    self.return_key(val, node.l)

if(node.r != None):
    self.return_key(val, node.r)

递归调用与其他函数调用没有什么不同,如果有返回值,则仍需要处理返回值.使用return语句:

Recursive calls are no different from other function calls, you still need to handle the return value if there is one. Use a return statement:

if(node.l != None):
    return self.return_key(val, node.l)

# ...

if(node.r != None):
    return self.return_key(val, node.r)

请注意,由于None是单例值,因此您可以并且应该在此处使用is not None来测试是否不存在:

Note that since None is a singleton value, you can and should use is not None here to test for the absence:

if node.l is not None:
    return self.return_key(val, node.l)

# ...

if node.r is not None:
    return self.return_key(val, node.r)

我怀疑您将错误的论点传递给了电话,但从此开始;如果第二个参数是节点,则不要传入节点值:

I suspect you are passing in the wrong arguments to the call to begin with however; if the second argument is to be a node, don't pass in the node value:

print(tree.return_key(6, tree.getRoot())) # drop the .v

另外,如果您所有的node类都具有相同的方法,则可以递归到该方法,而不用使用self.return_value();在Tree上执行以下操作:

Also, if all your node classes have the same method, you could recurse to that rather than using self.return_value(); on the Tree just do:

print tree.return_key(6)

其中Tree.return_key()委托给根节点:

def return_key(self, val):
    root = tree.getRoot()
    if root is not None:
        return tree.getRoot().return_key(val)

Node.return_key()变为:

def return_key(self, val):
    if val < self.v:
        if self.l is not None:
            return self.l.return_key(val)
    elif val > self.v:
        if self.r is not None:
            return self.r.return_key(val)

    # val == self.v or child node is None
    return self

我也在这里更新了val测试逻辑;如果val < self.v(或代码中的val < node.v)为假,则不要假定val > self.v为真; val可以等于.

I updated the val testing logic here too; if val < self.v (or val < node.v in your code) is false, don't assume that val > self.v is true; val could be equal instead.

这篇关于如何从python中的递归函数返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆