繁体   English   中英

Python树递归

[英]Python Tree Recursion

我在让python递归打印搜索树中的结果时遇到了一些困难。 我是C ++的本地人,我完全熟悉使用指针遍历此类结构,但是Python的工作量比以前更多。

无论如何,我希望有人能够帮助我。 我正在努力实施启发式方法来解决旅行商问题。 但是,直到我可以遍历我的树之前,我才能开始进行实际的启发式工作。 无论如何,这是树的代码。

class Tree:
    branches = dict()

    def __init__(self, cities, n=0):
        if len(cities) == 1:
            nc = list(cities)  # create a working list
            # grab the nth element of the list, default to head
            # Stash that as the node value
            self.node = nc[n]
            print "Complete!"
        elif len(cities) == 0:
            print "Doubly Complete!"
        else:
            nc = list(cities)  # create a working list
            # grab the nth element of the list, default to head
            # Stash that as the node value
            self.node = nc[n]
            print self.node
            del nc[n]  # Pop off the nth value from the list
            print "deleted city! See?"
            print nc
            c = 0  # create a counter
            for a in nc:  # loop through the remaining cities
                self.branches[a] = Tree(nc, c)  # generate a new tree
                c += 1  # increase the counter


    def __repr__(self, tier=1):
        ret = ("\t" * tier)
        ret += self.node
        ret += "\n"
        for a in self.branches:
            ret += self.branches[a].__repr__(tier+1)
        return ret

__str__ = __repr__

这是实例化并打印树的地方:

l = ['A','B','C','D']
mine = Tree(l)
print mine

打印树的结果是RuntimeError: maximum recursion depth exceeded 关于下一步该做什么,我无能为力。 我当然会感谢您的帮助!

哦! 当我说我相信时,如果我可以使用除递归之外的其他方法,那我会的。 但是,这种特殊的启发式方法是必需的。

感谢您提供的所有帮助!

此代码似乎有效。 我所做的就是将self.branches移到__init__内部。 我这样做是在调试时遵循最佳实践。 问题是他们是class members而不是instance members 让像dict这样的可变数据类型成为class member只是要求问题。

class Tree:
    def __init__(self, cities, n=0):
        self.branches = dict()
        if len(cities) == 1:
            nc = list(cities)  # create a working list
            # grab the nth element of the list, default to head
            # Stash that as the node value
            self.node = nc[n]
            print "Complete!"
        elif len(cities) == 0:
            print "Doubly Complete!"
        else:
            nc = list(cities)  # create a working list
            # grab the nth element of the list, default to head
            # Stash that as the node value
            self.node = nc[n]
            print self.node
            del nc[n]  # Pop off the nth value from the list
            print "deleted city! See?"
            print nc
            c = 0  # create a counter
            for a in nc:  # loop through the remaining cities
                self.branches[a] = Tree(nc, c)  # generate a new tree
                c += 1  # increase the counter

    def __repr__(self, tier=1):
        ret = ("\t" * tier)
        ret += self.node
        ret += "\n"
        for a in self.branches:
            ret += self.branches[a].__repr__(tier+1)
        return ret

    __str__ = __repr__

t = Tree(["SLC", "Ogden"], 1)
print t

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM