Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

C# tree data structure, making output

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:	emp" + s + ".xls"))
            { 
                streamWriter.Write("No.	MsgName	MsgData1	MsgData2	MsgData3");
                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
            }
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...