Python树数据类型在递归子代中添加子代运算结果

时间:2018-09-15 11:10:13

标签: python python-2.7 recursion data-structures tree

我有一个在python生成的树,我只是想将一个新的子代添加到父代,但是,尽管子代no的子代为空,但似乎发生了子节点成为父代和自身的子代的情况。

def __init__(self):
    self.child_nodes = []
    self.data = None
    ...
    self.parent = None

def init_tree(self, data, child_nodes=[], ...):
    self.child_nodes = child_nodes
    self.data = data
    ...

def add_child_node(self, node):
    ancestors = self.get_ancestors()
    if node is not self and node not in ancestors:
        self.child_nodes.append(node)
        node.parent = self

def get_ancestors(self):
    ancestor = self.parent
    ancestors = [ancestor]
    try:
        while ancestor.parent != None:
            ancestors.append(ancestor)
            parent = self.parent
            ancestor = parent.get_parent()
        return ancestors
    # Node is root. Root's parent is None
    except AttributeError:
        return []


def my_funct(self):
    ...
    child_node = RandomWalkTree()       
    child_node.init_tree(data=data)
    self.add_child_node(child_node)

my_funct导致一个子节点,其中一个child_nodes递归包含其自身。我缺少哪一点?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,并分享了类似的解决方案。初始化函数中的行

{"errors":[{"code":215,"message":"Bad Authentication data."}]}

由于这种情况,我将其更改为

curl --request POST --url 'https://api.twitter.com/1.1/account_activity/all/streaming/webhooks.json?url=https%3A%2F%{my-domain}%2Fwebhook%2Ftwitter' --header 'authorization: OAuth oauth_consumer_key="my-consumer-key", oauth_nonce="GENERATED", oauth_signature="GENERATED", oauth_signature_method="HMAC-SHA1", oauth_timestamp="GENERATED", oauth_token="my-token", oauth_version="1.0"'

并删除该参数。问题解决了。