Data structure for category

前端 未结 2 1993
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 04:27

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

For example:

Books

  • Drama
  • Science fic
相关标签:
2条回答
  • 2021-01-16 05:16

    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; }
    }
    
    0 讨论(0)
  • 2021-01-16 05:20

    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.

    0 讨论(0)
提交回复
热议问题