# 如何使用集合實現類別層次結構

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.

## 最佳答案

Why does it have to be collection-based? I would just create class Category with properties Category Parent and IList Children. 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.