简体   繁体   English

从树数据中查找根节点

[英]finding the root node from tree data

I have the following class: 我有以下课程:

class Category(object):
    def __init__(self, *args, **kwargs):
        self.id = kwargs.get('id')
        self.name = kwargs.get('name')
        self.parent_id = kwargs.get('parent_id', None)

    def get_top_parent_category(self, obj_list):
        # This algorithm is using extensive resource 
        category = self
        while category.parent_id:
            def filter_func(obj):
                return obj.id == category.parent_id
            parent = list(filter(filter_func, obj_list))

            category = parent[0]
        return category

    @classmethod
    def all(cls):
        url = BASE_URL + '/api/v1/categories/'
        response = requests.get(url, headers=headers, verify=False)
        if not response.status_code == 200:
            raise Exception('Error')
        categories = response.json()
        _temp_categories = []
        for _i in categories['results']:
            _temp_categories.append(cls(**_i))
        return _temp_categories

I am getting all categories by: 我按以下方式获取所有类别:

all_categories = Category.all()

Now I need to find root node of any Category provided. 现在,我需要找到提供的任何类别的根节点。

category = Category(**category_data)
category.get_top_parent_category(all_categories)

I get the desired results but i feel there might be some better way to find the root node using Graph Theory 我得到了预期的结果,但是我觉得可能会有更好的方法使用图论来找到根节点

What can be the better approach solving this problem? 解决这个问题的更好方法是什么?

If you need to do any more tree-related processing on it, you might want to link the Category objects to each other, instead of the indirection via the parent identifier. 如果您需要对其进行更多与树相关的处理,则可能需要将Category对象彼此链接,而不是通过父标识符进行间接链接。

But in the code you post the main problem is these repeated calls where you have to scan the entire object list: 但是在您发布的代码中,主要问题是这些重复的调用,您必须在其中扫描整个对象列表:

parent = list(filter(filter_func, obj_list))

If you replace this with a dictionary, your performance will be a lot better, since the lookup time for a single parent will be ~ constant time 如果将其替换为词典,则性能会好很多,因为单亲的查找时间为〜恒定时间

eg just for example 例如只是

parent_map = dict([(c.id, c) for c in obj_list])

(obviously don't do this inside the get_top_parent_category() method, since it's just as expensive) (显然,不要在get_top_parent_category()方法中执行此操作,因为它同样昂贵)

Then looking up the parent of a category can be done with a simple: 然后,可以通过以下简单方法查找类别的父项:

parent = parent_map[parent.id]

The same loop you have right now would be an order of magnitude faster this way. 您现在拥有的同一循环将以这种方式快一个数量级。

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

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