简体   繁体   English

如何使用集合实现类别层次结构

[英]How to implement a category hierarchy using collections

I have about 200 categories that are nested. 我有大约200个嵌套的类别。 I am currently reading the documention on the C5 generics library. 我目前正在阅读有关C5泛型库的文档。 I am not sure if the C5 library is overkill or not. 我不确定C5库是否过大。 I am looking at converting all my custom algorithms to the C5 implemention. 我正在考虑将所有自定义算法转换为C5实现。

This is what I need. 这就是我所需要的。 If a certain category is chosen i need to find its parents, siblings, direct children, and all children. 如果选择某个类别,我需要找到其父母,兄弟姐妹,直系子女和所有子女。

This is the way I have it set up. 这是我设置的方式。 To find the: 要找到:

Parents: I start from the current location then loop through the list and find the current parent. 父母:我从当前位置开始,然后遍历列表,找到当前父母。 When I find the parent I loop through the whole list again to find the next parent and so on. 当我找到父母时,我再次遍历整个列表以查找下一个父母,依此类推。

Siblings: I loop through the whole list and find all the nodes that have the same parent as the choosen node. 兄弟姐妹:我遍历整个列表,找到与所选节点具有相同父级的所有节点。

direct children: I loop through the whole list and find all nodes that is a parent of the choosen node. 直接子代:我遍历整个列表,找到作为所选节点父代的所有节点。

All Children: This one took me a while to figure out. 所有的孩子:这个花了我一段时间才弄清楚。 But I used recursion to find all children of the choosen node. 但是我使用递归来查找所选节点的所有子代。

Is there a better way to implement something like this? 有没有更好的方法来实现这样的事情?

Why does it have to be collection-based? 为什么必须基于集合? I would just create class Category with properties Category Parent and IList<Category> Children . 我只用属性Category ParentIList<Category> Children创建类Category That way, finding parent or predecessors is really easy (and fast!), finding children too, siblings are children of parent except the current category, so they are easy too. 这样,找到父母或前辈真的很容易(而且很快!),找到孩子也很容易,兄弟姐妹是除当前类别之外的父母的孩子,所以他们也很容易。 And finding descendants is trivial recursion. 而找到后代则是微不足道的递归。

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

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