且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#树数据结构,输出

更新时间:2022-10-27 09:21:35

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



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



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


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 *** already.