具有多个类的数据结构(树) [英] A data structure(tree) with multiple classes
问题描述
我遇到了一个问题,我有一个类别和项目的任务,我需要制作一个菜单树列表,它可以是N深,父母可以有他们想要的孩子:
菜单树如下所示:
- 类别(6)
- 类别(3)
- 项目(€0.50)
- 类别(2)
- 项目(€1.10)
- 项目(€6.25)
- 类别(1)
- 项目(€3.33)
- 类别(2)
- 项目(€1.10)
- 项目(€9.99)
- 类别(0)
- 类别(2)
- 类别(1)
- 项目(€0.50)
- 类别(1)
- 项目(€0.60)
该物品可以有物业 - 名称,活动,价格。该类别可以有 - 名称,活动。
我可以建议我应该使用哪种树吗?我是否必须为此树创建2个表,因为在我看来,类别不应该对价格有任何了解,也可以让Item打开新的更改,例如添加新字段。
我尝试过的事情:
我只进行了一项研究,我认为我可以找到一个可能有意见的人做了类似的事情,或者有任何线索我应该使用什么样的数据结构。
知道树的项节点总是叶节点特定类型使您的任务更简单。我将发布一个结构的草图,它将实现我知道可行的这种树。我说草图是因为我的目标是帮助你发展你的编程技巧......所以,我要留下代码的关键部分供你解决。使用System.Collections.Generic;
namespace YourNameSpace
{
{
public interface IMenu
{
string Name {get;组; }
bool IsActive {get;组; }
}
公共类MenuTree
{
public MenuTree(string name)
{
Name = name;
类别=新列表<类别>();
}
string Name {get;组; }
列表<类别>类别{get;组; }
公共类别AddCategory(类别猫)
{
//这里会发生什么?
}
}
公共类别类别:IMenu
{
公共类别(字符串名称,bool isActive = true)
{
Name = name;
IsActive = isActive;
MenuItems = new List< MenuTreeItem>();
类别=新列表<类别>();
}
public string Name {get;组; }
public bool IsActive {get;组; }
public List< Category>类别{get;组; }
public List< MenuTreeItem> MenuItems {get;组; }
公共类别AddCategory(类别猫)
{
//这里会发生什么?
}
public void AddMenuItem(MenuTreeItem itm)
{
//这里会发生什么?
}
}
公共类MenuTreeItem:IMenu
{
public MenuTreeItem(string name,double price,bool isActive = true)
{
//这里发生了什么?
}
//参见注释#1
public Category MItemCategory {get;组; }
//必须定义其他字段或属性吗?
}
}如果此代码完成,可以像这样使用:
MenuTree mTree = new MenuTree( TestMenuTree);
int id = 1;
for(int i = 0; i< 5; i ++)
{
Category cat1 = mTree.AddCategory(new Category(
< BLOCKQUOTE> Cat_ {I}));
for(int j = 0; j< 3; j ++)
{
cat1.AddMenuItem(new MenuTreeItem(
mi_ {id ++},(j * 11.11)+ 22));
}
类别cat2 = cat1.AddCategory(新类别(
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.
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(
这篇关于具有多个类的数据结构(树)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!