I was looking for a tree or graph data structure in C# but I guess there isn\'t one provided. An Extensive Examination of Data Structures Using C# 2.0 explains a bit about w
I have added complete solution and example using NTree class above, also added "AddChild" method...
public class NTree
{
public T data;
public LinkedList> children;
public NTree(T data)
{
this.data = data;
children = new LinkedList>();
}
public void AddChild(T data)
{
var node = new NTree(data) { Parent = this };
children.AddFirst(node);
}
public NTree Parent { get; private set; }
public NTree GetChild(int i)
{
foreach (NTree n in children)
if (--i == 0)
return n;
return null;
}
public void Traverse(NTree node, TreeVisitor visitor, string t, ref NTree r)
{
visitor(node.data, node, t, ref r);
foreach (NTree kid in node.children)
Traverse(kid, visitor, t, ref r);
}
}
public static void DelegateMethod(KeyValuePair data, NTree> node, string t, ref NTree> r)
{
string a = string.Empty;
if (node.data.Key == t)
{
r = node;
return;
}
}
using
NTree> ret = null;
tree.Traverse(tree, DelegateMethod, node["categoryId"].InnerText, ref ret);