0

C#のツリーデータ構造に従って、次のツリー構造クラスを実装しようとしています

delegate void TreeVisitor<T>(T nodeData);

class NTree<T>
{
    T data;
    LinkedList<NTree<T>> children;

    public NTree(T data)
    {
        this.data = data;
        children = new LinkedList<NTree<T>>();
    }

    public void addChild(T data)
    {
        children.AddFirst(new NTree<T>(data));
    }

    public NTree<T> getChild(int i)
    {
        foreach (NTree<T> n in children)
            if (--i == 0) return n;
        return null;
    }

    public NTree<T> getChild( T data )
{
    foreach (NTree<T> n in children)
        {
         if (n.data.Equals(data))
     {
         return n;
     }
    }
    return null;
    }

    public void traverse(NTree<T> node, TreeVisitor<T> visitor)
    {
        visitor(node.data);
        foreach (NTree<T> kid in node.children)
            traverse(kid, visitor);
    }        
}

オブジェクトのインスタンスを XML として出力したいのですが、子と親の階層を維持するのに苦労しています。

これまでのところ、

NTree<string> root = new NTree<string>( "Electronic" );
root.addChild( "Techno" );
root.getChild( "Techno" ).addChild( "Detroit" );
root.getChild( "Techno" ).addChild( "Gabba" );
root.addChild( "House" );
root.getChild( "House" ).addChild( "Deep" );
root.getChild( "House" ).addChild( "Ambient" );
root.getChild( "House" ).addChild( "Chicago" );

XElement treeAsXml = new XElement("Root");
root.traverse( root, new TreeVisitor<string>( this.ConvertNodeToXml ) );

private void ConvertNodeToXml( string nodeData )
{
 XElement node = new XElement( "Node" );
 node.Value = nodeData;
 this.treeAsXml.Add( node );
}

それは私に与えます:

<Root><Node>Electronic</Node><Node>House</Node><Node>Chicago</Node><Node>Ambient</Node><Node>Deep</Node><Node>Techno</Node><Node>Gabba</Node><Node>Detroit</Node></Root>

理想的には以下のように、これを正しく出力するにはどうすればよいですか

<Node value="Electronic">
<Node value="Techno">
    <Node value="Detroit" />
    <Node value="Gabba" />
</Node> 
<Node value="House">
    <Node value="Deep" />
    <Node value="Ambient" />
    <Node value="Chicago" />
</Node>     
</Node>
4

1 に答える 1

1

問題は、デリゲートにコンテキストがなく、ノードのデータのみを取得するため、それが親であり、子であることがわからないことです。何らかのタイプのコンテキストを含めるようにデリゲートを変更するか、拡張メソッドとして実行する必要があります。

public static class NTreeXmlHelper
{
    public static XElement TreeAsXml<T>(this NTree<T> node)
    {
        XElement element = new XElement("Node",
                                  new XAttribute("value", node.data));

        foreach (var child in node.children)
        {
            element.Add(TreeAsXml(child));
        }

        return element;
    }
}

これが機能するには、プロパティchildrendataプロパティを公開する必要があります。

それを使用するには、次のようにします。

        NTree<string> root = new NTree<string>("Electronic");
        root.addChild("Techno");
        root.getChild("Techno").addChild("Detroit");
        root.getChild("Techno").addChild("Gabba");
        root.addChild("House");
        root.getChild("House").addChild("Deep");
        root.getChild("House").addChild("Ambient");
        root.getChild("House").addChild("Chicago");

        treeAsXml = root.TreeAsXml();
        Console.WriteLine(treeAsXml.Tostring());
于 2012-08-29T11:51:20.400 に答える