• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
周巍
学习生活,学习技术,也学习英语
博客园    首页    新随笔    联系   管理    订阅  订阅

Create a tree from a flatten collection

Code
public static class CollectionExtensions
{
    
public static ICollection<T> BuildTree<T, TKey>(
        
this ICollection<T> collection,
        Func
<T, TKey> getKey,
        Func
<T, TKey> getParentKey,
        Func
<T, ICollection<T>> getChildren,
        TKey topestParentKey)
    
{
        
for (int i = 0; i < collection.Count; i++)
        
{
            T element 
= collection.ElementAt(i);
            TKey elementKey 
= getKey(element);

            
foreach (var member in collection)
            
{
                
if (object.Equals(getParentKey(member), elementKey))
                
{
                    getChildren(element).Add(member);
                }

            }

        }


        
for (int i = 0; i < collection.Count; i++)
        
{
            T element 
= collection.ElementAt(i);
            TKey elementKey 
= getKey(element);

            
if (!object.Equals(getParentKey(element), topestParentKey))
            
{
                collection.Remove(element);
                
--i;
            }

        }


        
return collection;
    }

}

Sample
static class Program
{
    
static void Main(string[] args)
    
{
        List
<Node> nodes = BuildNodes();
        Console.WriteLine(
"Total: {0}", nodes.Count);

        var result 
= nodes.BuildTree(
            node 
=> node.ID,
            node 
=> node.Parent == null ? null : node.Parent.ID,
            node 
=> node.Children,
            
null)
            .OrderBy(node 
=> node.ID);

        
int total = 0;
        Write(result, 
ref total);
        Console.WriteLine(
"Total: {0}", total);

        Console.Write(
"Press any key to exit");
        Console.ReadKey(
true);
    }


    
static void Write(IEnumerable<Node> nodes, ref int total)
    
{
        
foreach (var node in nodes)
        
{
            Console.WriteLine(node.ID);
            
++total;

            Write(node.Children, 
ref total);
        }

    }


    
static List<Node> BuildNodes()
    
{
        Random random 
= new Random();
        List
<Node> allNodes = new List<Node>();

        
for (int i = 1; i <= 3; i++)
        
{
            Node first 
= new Node()
            
{
                ID 
= string.Format("First {0}", i),
                Order 
= random.Next(1, 1000)
            }
;
            allNodes.Add(first);

            
for (int j = 1; j <= 3; j++)
            
{
                Node second 
= new Node()
                
{
                    ID 
= string.Format("Second {0} {1}", i, j),
                    Order 
= random.Next(1, 1000),
                    Parent 
= first
                }
;
                allNodes.Add(second);

                
for (int k = 1; k <= 3; k++)
                
{
                    Node third 
= new Node()
                    
{
                        ID 
= string.Format("Third {0} {1} {2}", i, j, k),
                        Order 
= random.Next(1, 1000),
                        Parent 
= second
                    }
;
                    allNodes.Add(third);
                }

            }

        }


        
return allNodes.OrderBy(node => node.Order).ToList();
    }

}


class Node
{
    
public string ID;
    
public Node Parent;
    
public int Order;
    
public readonly List<Node> Children = new List<Node>();
}
posted @ 2009-08-19 00:52  周巍  阅读(215)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3