2010-08-11 76 views
2

如何從使用LINQ的樹中的所有節點獲取列表?如何從使用LINQ的樹中的所有節點獲取列表?

我的類別是:

class Node 
{ 
public class Node() 
{ 
    Children = new List<Node>(); 
} 

public List<Node> Children { get; set;} 
} 

class Tree 
{ 
public Tree() 
{ 
    Roots = new List<Node>(); 
} 

List<Node> Roots { get; set;} 
} 
+0

這功課嗎? – leppie 2010-08-11 12:53:08

+0

不,它不是, 我有一個對象樹,我需要做一些查詢,就是它 – Homam 2010-08-11 12:57:09

+0

請儘可能樣本 – DEVMBM 2010-09-18 01:25:49

回答

3
class Node 
    { 
    public Node() 
    { 
     Children = new List<Node>(); 
    } 

    public IEnumerable<Node> GetSubTree() 
    { 
     return Children.SelectMany(c => c.GetSubTree()).Concat(new[] { this }); 
     //Post-order traversal 
    } 

    public List<Node> Children { get; set; } 
} 

class Tree 
{ 
    public Tree() 
    { 
     Roots = new List<Node>(); 
    } 

    public IEnumerable<Node> GetAllNodes() 
    { 
     return Roots.SelectMany(root => root.GetSubTree()); 
    } 

    List<Node> Roots { get; set; } 
} 

樹怎麼能有一個以上的根雖然?這不是森林嗎?

+0

@Homan Func <樹,IEnumerable > nodesForTree = tree => tree.GetAllNodes(); 這就是你想要的嗎?爲什麼? – Ani 2010-08-11 13:10:07

+0

感謝您的迴應,我將使用您的代碼,我希望在不更改類體的情況下進行查詢,換句話說,在查詢之前創建函數GetSubTree,並在不改變類的情況下在查詢中使用它。 無論如何,再次感謝你:) – Homam 2010-08-11 22:03:02

+0

@霍曼:我明白了。如果這是您的要求,您可以輕鬆地將這些方法放在單獨的類中,因爲它們只需要訪問公共屬性。您可以將它們標記爲擴展方法來簡化使用語法。 – Ani 2010-08-12 15:58:59

3
var allNodes = yourTree.Roots.SelectMany(x => x.TraverseTree(y => y.Children)); 

// ... 

public static class EnumerableExtensions 
{ 
    public static IEnumerable<T> TraverseTree<T>(
     this T parentNode, Func<T, IEnumerable<T>> childNodesSelector) 
    { 
     yield return parentNode; 

     IEnumerable<T> childNodes = childNodesSelector(parentNode); 
     if (childNodes != null) 
     { 
      foreach (T childNode in 
       childNodes.SelectMany(x => x.TraverseTree(childNodesSelector))) 
      { 
       yield return childNode; 
      } 
     } 
    } 
} 
相關問題