Tree data structure in C#

前端 未结 20 2672
梦如初夏
梦如初夏 2020-11-22 08:30

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

20条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 09:09

    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);
    

提交回复
热议问题