Data structure for category

旧街凉风 提交于 2019-12-20 06:07:28

问题


I am looking for a data structure to add, remove, get, and find on categories.

For example:

Books

  • Drama
  • Science fiction
  • Other

Sports

  • Cycling
  • Golf
  • Team Sports
    • Soccer
    • Football

etc.

I think about using tree from the C5 collection library for example, but it looks like it has only red-black trees. Any suggestions?


回答1:


You can just create a Category class that exposes a list of other Category instances.

public class Category
{
    public Category()
    {
        this.ChildCategories = new List<Category>();
    }

    public string Name { get; set; }

    public IList<Category> ChildCategories { get; private set; }
}



回答2:


A tree would be a good approach, but I get the feeling you are thinking there is going to be a one-size-fits-all data structure that you could use and that is not really how I envision it. I concur with Mark's solution, but recommend a Dictionary instead of a List. That way you can lookup a Category and get its subcategories quickly.



来源:https://stackoverflow.com/questions/1328375/data-structure-for-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!