C#树数据结构,输出 [英] C# tree data structure, making output

查看:203
本文介绍了C#树数据结构,输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过类制作了我自己的树数据结构。
现在我被卡住了很多基本的东西。我需要在我的列表< MainTreeNode> 中的数据中输出选项卡分隔的文件。

我认为递归是只有这样??



树是N-tree,输出有第一行作为标题,其他行是值。



树:



  1. MSG(MainTreeNode)


    • MainParam(必须有prop NAME,不必具有参数VALUE)


      • SubParam1(必须具有道具名称,必须有道具价值)

      • SubParam2(必须具有道具名称,不必具有权值VALUE)


        • SubSubParam2.1(必须有道具名称,必须有道具价值)






或: / strong>



  1. 消息:名称


    • Param1:ValueV1(VALUE必须,因为它没有孩子)

    • Param2


      • Param2.1:ValueV2

      • Param2.2:Value


        • Param2.2.1:ValueV3

        • Param2.2.2:ValueV4 ... etc 。





输出必须这样(第一行是标题):

  Param1 | Param2 / Param2.1 | Param2 / Param2.2 / Param2.2.1 | Param2 / Param2.2 / Param2.2.2 
ValueV1 | ValueV2 | ValueV3 | ValueV4
..

所以我需要可能的标题列表和值,但是我不知道如何实现



某些未完成的代码:

  public void PrintToTabFile(List< Message>消息,列表< string>参数)
{
foreach(参数中的字符串)
{
使用(StreamWriter streamWriter = new StreamWriter(@C:\temp\+ s +)。 xls))
{
streamWriter.Write(No.\tMsgName\tMsgData1\tMsgData2\tMsgData3);
var msg = messages.Where(x => x.Parameters.Where(p => p.ParameterName == s).Count()== 1);

列表< string> headers = new List< string>();
列表< string> values = new List< string>();
// ... Stuck !!!
}
}
}

private void递归(参数参数,列表< string>标题,列表< string>值)
{
if(parameter.SubParameters.Count == 0)
{
int index = headers.IndexOf(parameter.ParameterName);
values [index] = parameter.ParameterValue;
}
else
{
foreach(parameter.SubParameters中的参数p)
{
递归(p,headers,values);
//和Stuck可能在此或之前递归调用
}
}
}


解决方案

我不会声称我真的知道你在问什么,但是我仍然会给它拍摄。对我来说,这似乎是一个非常基本的递归,当您正在执行时遍历一个奇怪的输出的树结构。
尝试这样:
1.为所有的类编写一个名为INode的接口。这个界面应该包含一些典型的东西,如List Children,INode Parent等。
2.让所有的类实现这个接口(或让它们继承一个这样做的基类)
3.现在从你的使用儿童属性递归地遍历所有儿童,并生成您的输出。



这应该是我猜想的技巧。 (对不起,没有VS在这里提出一些真正的代码)



BTW:你可能会在stackoverflow上找到大量关于这个的帖子。


I've made my own tree data structure via classes. Now I'm stuck with really basic stuffs. I need to make output tab delimited file from data in my List <MainTreeNode>.
I think that recursion is only way?!

Tree is N-tree, and output have first row as header and other rows are values.

Tree:

  1. MSG (MainTreeNode)
    • MainParam (Must have prop NAME, doesn't have to have prop VALUE)
      • SubParam1 (Must have prop NAME, must have prop VALUE)
      • SubParam2 (Must have prop NAME, doesn't have to have prop VALUE)
        • SubSubParam2.1 (Must have prop NAME, must have prop VALUE)
          etc.

Or:

  1. Message : Name
    • Param1 : ValueV1 (VALUE must, because it doesn't have children)
    • Param2
      • Param2.1 : ValueV2
      • Param2.2 : Value
        • Param2.2.1 : ValueV3
        • Param2.2.2 : ValueV4 ...etc.

And output have to be like this (first line is header):

Param1|Param2/Param2.1|Param2/Param2.2/Param2.2.1|Param2/Param2.2/Param2.2.2  
ValueV1|ValueV2|ValueV3|ValueV4
...

So I need probably List for header and for values but I don't know how to implement that in recursion way (or any another).

Some of unfinished code:

public void PrintToTabFile(List<Message> messages, List<string> parameters)
    {
        foreach (string s in parameters)
        {
            using (StreamWriter streamWriter = new StreamWriter(@"C:\temp\" + s + ".xls"))
            { 
                streamWriter.Write("No.\tMsgName\tMsgData1\tMsgData2\tMsgData3");
                var msg = messages.Where(x => x.Parameters.Where(p => p.ParameterName == s).Count() == 1);

                List<string> headers = new List<string>();
                List<string> values= new List<string>();
                //... Stuck!!!
            }
        }
    }

    private void Recursion(Parameter parameter, List<string> headers, List<string> values)
    {
        if (parameter.SubParameters.Count == 0)
        {
            int index = headers.IndexOf(parameter.ParameterName);
            values[index] = parameter.ParameterValue;
        }
        else
        {
            foreach (Parameter p in parameter.SubParameters)
            {
                Recursion(p, headers, values);
                //and Stuck Probably here or before recursion call
            }
        }
    }

解决方案

I won't claim that I really know what you are asking, but still I'll give it shot. To me this seems like a very basic recursion to traverse a tree structure with some weird output while you are doing it. Try it like this: 1. Make an interface for all your classes called INode. This interface should contain the typical things like List Children, INode Parent etc. 2. Make all your classes implement this interface (or let them inherit a base class that does this) 3. Now start with your base clase and traverse recursively over all Children using the Children property and generate your output.

This should do the trick I guess. (Sorry, no VS here to put up some real code)

BTW: you'll probably find a ton of posts about this on stackoverflow already.

这篇关于C#树数据结构,输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆