寻找一个好的Python Tree数据结构 [英] Looking for a good Python Tree data structure

查看:33
本文介绍了寻找一个好的Python Tree数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个好的 Tree 数据结构类.我遇到过这个包,但由于我对 Python 比较陌生(不是编程),我不知道有没有更好的.

I am looking for a good Tree data structure class. I have come across this package, but since I am relatively new to Python (not programming), I dont know if there are any better ones out there.

我想听听这里的 Pythonistas 的意见 - 您有经常使用并推荐的最喜欢的树脚本吗?

I'd like to hear from the Pythonistas on here - do you have a favorite tree script that you regularly use and would recommend?

为了澄清,树",我的意思是一个简单的无序树(嗯,这有点递归定义 - 但希望能在某种程度上澄清事情).关于我需要树的用途(即用例).我正在从平面文件中读取树数据,我需要根据数据构建一棵树并遍历树中的所有节点.

To clarify, by 'Tree', I mean a simple unordered tree (Hmm, thats a bit of a recursive definition - but hopefully, that clarifies things somewhat). Regarding what I need the tree for (i.e. use case). I am reading tree data from a flat file and I need to build a tree from the data and traverse all nodes in the tree.

推荐答案

自行解决.例如,只需将您的树建模为列表列表.在人们提供更好的推荐之前,您应该详细说明您的具体需求.

Roll your own. For example, just model your tree as list of list. You should detail your specific need before people can provide better recommendation.

针对 HelloGoodbye 的问题,这是一个迭代树的示例代码.

In response to HelloGoodbye's question, this is a sample code to iterate a tree.

def walk(node):
    """ iterate tree in pre-order depth-first search order """
    yield node
    for child in node.children:
        for n in walk(child):
            yield n

一个问题是这个递归实现是 O(n log n).它适用于我必须处理的所有树木.也许 Python 3 中的子生成器会有所帮助.

One catch is this recursive implementation is O(n log n). It works fine for all trees I have to deal with. Maybe the subgenerator in Python 3 would help.

这篇关于寻找一个好的Python Tree数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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