简体   繁体   English

创建树:递归或表达式树C#

[英]Create Tree : Recursion or Expression Tree C#

I have three DataSets. 我有三个数据集。 DataSet 1 has all the parent Ids, DataSet 2 has children of DataSet 1 and DataSet 3 has children of DataSet3. DataSet 1具有所有父ID,DataSet 2具有DataSet 1的子代,DataSet 3具有DataSet3的子代。

I want to build a DataSet of tree like structures with Id of DataSet1 as root node. 我想使用ID为DataSet1的根节点构建树状结构的数据集。

DataSet 1- 数据集1-

<NewDataSet>
    <Table>
        <Id>A</Id>
        <Desc>ABC</Desc>
    </Table>
    <Table>
        <Id>B</Id>
        <Desc>DEF</Desc>
    </Table>
    <Table>
        <Id>C</Id>
        <Desc>PQR</Desc>
    </Table>
</NewDataSet>

DataSet 2- 数据集2

<NewDataSet>
    <Table>
        <ParentId>A</ParentId>
        <Id>AA</Id>
        <Desc>ABC</Desc>
    </Table>
    <Table>
        <ParentId>B</ParentId>
        <Id>BB</Id>
        <Desc>DEF</Desc>
    </Table>
    <Table>
        <ParentId>B</ParentId>
        <Id>CB</Id>
        <Desc>PQR</Desc>
    </Table>
</NewDataSet>

DataSet 3- 数据集3-

<NewDataSet>
    <Table>
        <ParentId>AA</ParentId>
        <Id>AAA</Id>
        <Desc>ABC</Desc>
    </Table>
    <Table>
        <ParentId>BB</ParentId>
        <Id>BBB</Id>
        <Desc>DEF</Desc>
    </Table>
    <Table>
        <ParentId>BB</ParentId>
        <Id>CBB</Id>
        <Desc>PQR</Desc>
    </Table>
</NewDataSet>

Result Should be like this if don't specify any root node or if I want the children nodes or not: 结果如果未指定任何根节点,或者是否需要子节点,则应该是这样的:

GetTree(null, false); GetTree(null,false);

<NewDataSet>
    <Table>
        <Id>A</Id>
        <Desc>ABC</Desc>
        <HasChildren>True</HasChildren>
        <NewDataSet>
            <Table>
                <Id>AA</Id>
                <Desc>ABC</Desc>
                <HasChildren>True</HasChildren>
                <DataSet>
                    <Table>
                        <Id>AAA</Id>
                        <Desc>ABC</Desc>
                        <HasChildren>False</HasChildren>
                    </Table>    
                </DataSet>
            </Table>
        </NewDataSet>
    </Table>
    <Table>
    ..... //All Root Nodes and their children
    </Table>
</NewDataSet>

But If I Provide an Id and also provide if I want the Id's children. 但是,如果我提供ID并且还提供我是否需要ID的孩子。 GetTree("BB", true); GetTree(“ BB”,true);

<NewDataSet>
    <table>
        <Id>BB</Id>
        <Desc>ABC</Desc>
        <HasChildren>True</HasChildren>
        <DataSet>
            <Table>
                <Id>BBB</Id>
                <Desc>ABC</Desc>
                <HasChildren>False</HasChildren>
            </Table>
            <Table>
                <Id>CBB</Id>
                <Desc>PQR</Desc>
                <HasChildren>False</HasChildren>
            </Table>
        </DataSet>
    </table>
</NewDataSet>

GetTree("BB", false); GetTree(“ BB”,false);

<NewDataSet>
    <table>
        <Id>BB</Id>
        <Desc>ABC</Desc>
        <HasChildren>True</HasChildren>
    </table>
</NewDataSet>

I know I can do this with Nested Foreach but I want to write a recursive function to perform this operation. 我知道我可以使用嵌套的Foreach做到这一点,但是我想编写一个递归函数来执行此操作。 I have been searching the Internet for 2 days now for some examples and not able to do this. 我已经在互联网上搜索了2天,以获取一些示例,但无法执行此操作。 Any reference to a website would also help me alot. 对网站的任何引用也会对我有很大帮助。

Foreach is probably still the way to go, but instead of nested, you would just use it once for each 'node'. Foreach可能仍然是要走的路,但您不必为每个嵌套节点使用嵌套一次,而是对其进行嵌套。

public class MyDataSet
{
    public string ID {get;set;}
    public string Description {get;set;}
    public readonly List<MyDataSet> Children = new List<MyDataSet>();
    public bool HasChildren 
    {
        get { return Children.Count > 0 }
    }
    public void GetTree(string id, bool includeChildren)
    {
        MyDataSet mySet = id == null ? this : Children.FirstOrDefault(child => child.ID == id);

        if(mySet == null) return;
        // TODO: handle mySet

        if(includeChildren)
        {
            foreach (MyDataSet child in Children)
            {
                child.GetTree(null, true);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM