Data structure for category

倾然丶 夕夏残阳落幕 提交于 2019-12-02 12:38:22

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; }
}

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.

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