Click here to Skip to main content
15,889,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I ran to an issue, I have a task with categories and items, I need to make a menu tree list, it can be N deep, parent can have many children as he wants:

Menu tree looks like this:

- Category (6)
    - Category (3)
      - Item (€0.50)
      - Category (2)
        - Item (€1.10)
        - Item (€6.25)
      -Category (1)
        - Item (€3.33)
    - Category (2)
      - Item (€1.10)
      - Item (€9.99)
    - Category (0)
- Category (2)
    - Category (1)
        - Item (€0.50)
    -Category (1)
        - Item (€0.60)


The item can have properties - name, active, price. The category can have - name, active.

So could I have any suggestions what kind of tree should I be using ? Do I have to create 2 tables for this tree, because in my opinion category should not know anything about a price, also maybe make Item open to new changes, for example adding new fields.

What I have tried:

I only did only a research and I thought I could find an opinion who had maybe did something similar or have any clue what kind of data structure should I use.
Posted
Updated 24-Feb-18 5:19am
Comments
BillWoodruff 24-Feb-18 9:54am    
Is this a WinForms project ? When you say "menu," do you mean there's some visual UI that's part of this ?

Have you written any code ?

"create 2 tables for this tree" are you talking about a database table, here ?
Dntdothis 24-Feb-18 9:59am    
Yeah, 2 tables, well I did not wrote any code, since I want to have a clear vision how to do it from architecture side.
BillWoodruff 24-Feb-18 10:29am    
Is it the case that an Item Node will always be a leaf node: i.e., it will never contain other nodes ? That's yhe structure your code shows now.
Dntdothis 24-Feb-18 10:30am    
Yeah, it is like a product with price, so it would not contain childs

1 solution

Knowing your tree's item nodes are always leaf nodes of a specific type makes your task a bit simpler. I'm going to post a sketch of a structure that will implement a tree of this type which I know works. I say "sketch" because my goal is to assist you in developing your programming skills ... so, I am leaving key pieces of the code out for you to work out.
using System.Collections.Generic;

namespace YourNameSpace
{
{
    public interface IMenu
    {
        string Name { get; set; }
        bool IsActive { get; set; }
    }

    public class MenuTree
    {
        public MenuTree(string name)
        {
            Name = name;
            Categories = new List<Category>();
        }

        string Name { get; set; }

        List<Category> Categories { get; set; }

        public Category AddCategory(Category cat)
        {
            // what happens here ?
        }
    }

    public class Category : IMenu
    {
        public Category(string name, bool isActive = true)
        {
            Name = name;
            IsActive = isActive;
            MenuItems = new List<MenuTreeItem>();
            Categories = new List<Category>();
        }

        public string Name { get; set; }
        public bool IsActive { get; set; }

        public List<Category> Categories { get; set; }
        public List<MenuTreeItem> MenuItems { get; set; }

        public Category AddCategory(Category cat)
        {
             // what happens here ?
        }

        public void AddMenuItem(MenuTreeItem itm)
        {
             // what happens here ?
        }
    }

    public class MenuTreeItem : IMenu
    {
        public MenuTreeItem(string name, double price,  bool isActive = true)
        {
            // what happens here ?
        }

        // see note #1
        public Category MItemCategory { get; set; }

        // what other Fields or Properties must be defined ?
    }
}
If this code were complete, it could be used like this:
MenuTree mTree = new MenuTree("TestMenuTree");

int id = 1;

for (int i = 0; i < 5; i++)
{
    Category cat1 = mTree.AddCategory(new Category($"Cat_{i}"));

    for (int j = 0; j < 3; j++)
    {
        cat1.AddMenuItem(new MenuTreeItem($"mi_{id++}", (j * 11.11) + 22));
    }

    Category cat2 = cat1.AddCategory(new Category($"Cat_{i}_A"));

    for (int j = 0; j < 3; j++)
    {
        cat2.AddMenuItem(new MenuTreeItem($"mi_{id++}", (j * 23.45) + 56.45));
    }
}
note #1: Should the reference to the Item's 'Category be passed in the Item's constructor rather than be set by the 'AddMenuItem method of the Category ?
 
Share this answer
 
v2
Comments
Dntdothis 24-Feb-18 11:19am    
Thank You very much sir! Definitely will look into this! I will keep You updated, thanks once more!
Maciej Los 24-Feb-18 15:16pm    
Wow!
5ed!
BillWoodruff 24-Feb-18 15:49pm    
thanks, Maciej !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900