views:

173

answers:

2

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

for example:

books

  • drama
  • science fiction
  • other

Sports

  • Cycling
  • Golf
  • Team Sports
    • soccer
    • football etc.

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

+4  A: 

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; }
}
Mark Seemann
this is start feeling like a tree to me...how do you put the next item? for example add soccer node as a child of team-sports node which is a child of Sports child...[Why implement this... when you can use a tree...]
rabashani
+1 I like your answer, but use a Dictionary instead of a List.
Brian Gideon
@rabashani: Create a new Category instance and give it the Name "Soccer", then add it to the ChildCategories of another Category instance with the Name "Team Sports", etc.
Mark Seemann
+3  A: 

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.

Brian Gideon
interesting... but still there is and addition complexity...
rabashani