5

假设我有一个如下所示的 WinForms Treeview:

Parent1
   Child1
      Sub-Child1
         DeepestNode1
         DeepestNode2
         DeepestNode3
      Sub-Child2
         DeepestNode4
         DeepestNode5
         DeepestNode6
   Child2
      Sub-Child3
      Sub-Child4
      Sub-Child5
      Sub-Child6
   Child3
      (no children)

我想按照以下方式创建一个函数:

Function GetDeepestChildren(MyNode as Treenode) as List(Of Treenode)

如果结果如下所示:

GetDeepestChildren(Parent1) = {DeepestNode1, DeepestNode2, DeepestNode3, DeepestNode4, DeepestNode5, DeepestNode6}

GetDeepestChildren(Sub-Child1) = {DeepestNode1, DeepestNode2, DeepestNode3}

GetDeepestChildren(Child2) = {Sub-Child3, Sub-Child4, Sub-Child5, Sub-Child6}

GetDeepestChildren(Child3) = Empty list

... 换句话说,始终从给定的节点转到最深的级别并返回子节点 - 即使它们在不同的父节点之间拆分(如 中的情况Parent1)。

我创建了一个函数,它可以告诉我一个节点深入了多少层,如下所示:

    Public Function GetDeepestChildNodeLevel(ByVal ParentNode As TreeNode) As Integer
        Dim subLevel = ParentNode.Nodes.Cast(Of TreeNode).Select(Function(subNode) GetDeepestChildNodeLevel(subNode))
        Return If(subLevel.Count = 0, 0, subLevel.Max() + 1)
    End Function

所以我知道从什么级别得到孩子,我正在寻找的是一个可以做到这一点的功能 - Somethign 大致如下:

Function GetDeepestChildren(MyNode as Treenode) as List(Of Treenode)
       Return All child nodes where level = GetDeepestChildNodeLevel(MyNode)
End function

我希望这是有道理的-谢谢!

4

7 回答 7

4

在 C# 中,您可以使用yield return或使用递归 lambda。这是第二种方法的示例:

Func<TreeNode,IEnumerable<TreeNode>> getChildren = null;
getChildren = n => {
    if (n.Nodes.Count != 0) {
        var list = new List<TreeNode>(n.Nodes.Where(c => c.Nodes.Count == 0));
        foreach (var c in n.Nodes) {
            // Note the recursive call below:
            list.AddRange(getChildren(c));
        }
        return list;
    } else {
        return new TreeNode[0];
    }
};
var res = getChildren(myTree);
于 2013-01-03T15:31:02.673 回答
1

这是一个使用 XML 的版本——翻译应该很容易。我使用了我推荐的linqPad,你可以运行它并直接在 linkPad 中查看它的工作原理

WalkDeep(tree,getDeep(tree)) returns:

<DeepestNode1 /> 
<DeepestNode2 /> 
<DeepestNode3 /> 
<DeepestNode4 /> 
<DeepestNode5 /> 
<DeepestNode6 /> 

C# 代码更好,因为您可以使用 yield

VB代码

function getDeep( e as XElement) as integer
  if (e.HasElements)
    return 1 + e.Elements().Select(Function(c) getDeep(c)).Max()
  else
    return 1
  end if  
end function

function WalkDeep(root as XElement,find as integer,optional mylevel as integer = 1) as IEnumerable(of XElement)
  Dim result As New List(Of XElement)

  if find = mylevel 
    result.Add(root)
  else 
    if root.HasElements
      for each c as XElement in root.Elements()
        for each r as XElement in WalkDeep(c,find,mylevel+1)
            result.Add(r)
        next
      next  
    end if
  end if

  return result
end function

Sub Main
  dim tree as XElement = <Parent1>
     <Child1>
        <Sub-Child1>
           <DeepestNode1/>
           <DeepestNode2/>
           <DeepestNode3/>
        </Sub-Child1>   
        <Sub-Child2>
           <DeepestNode4/>
           <DeepestNode5/>
           <DeepestNode6/>
        </Sub-Child2>   
     </Child1>      
     <Child2>
        <Sub-Child3/>
        <Sub-Child4/>
        <Sub-Child5/>
        <Sub-Child6/>
     </Child2>   
     <Child3 />
  </Parent1>   

  WalkDeep(tree,getDeep(tree)).Select(function(x) x.Name.LocalName).Dump()
End Sub

C#代码:

int getDeep(XElement e)
{
  if (e.HasElements)
    return 1 + e.Elements().Select(c => getDeep(c)).Max();
  else
    return 1;
}

IEnumerable<XElement> WalkDeep(XElement root,int find, int mylevel=1)
{   
  if (find == mylevel) yield return root;

  if (root.HasElements)
  {
    foreach(XElement c in root.Elements())
    {
      foreach(XElement r in WalkDeep(c,find,mylevel+1))
        yield return r;

    }
  }

  yield break;
}

void Main()
{
  XElement tree = XElement.Parse (@"
  <Parent1>
     <Child1>
        <Sub-Child1>
           <DeepestNode1/>
           <DeepestNode2/>
           <DeepestNode3/>
        </Sub-Child1>   
        <Sub-Child2>
           <DeepestNode4/>
           <DeepestNode5/>
           <DeepestNode6/>
        </Sub-Child2>   
     </Child1>      
     <Child2>
        <Sub-Child3/>
        <Sub-Child4/>
        <Sub-Child5/>
        <Sub-Child6/>
     </Child2>   
     <Child3 />
  </Parent1>   
  ");

  WalkDeep(tree,getDeep(tree)).Dump();
} 
于 2013-01-03T16:26:00.623 回答
1

这是我根据@dasblinkenlight 的解决方案创建的 VB.Net 重新制作 - 它运行良好,我只是把它放在这里,以防将来有人需要 VB 中的解决方案。

    Public Function GetDeepestChildNodes(ByVal ParentNode As TreeNode) As List(Of TreeNode)
        Dim RetVal As New List(Of TreeNode)

        If ParentNode.Nodes.Count > 0 Then
            RetVal = (From nd As TreeNode In ParentNode.Nodes
                   Where nd.Nodes.Count = 0
                   Select nd).ToList

            For Each nd In ParentNode.Nodes
                RetVal.AddRange(GetDeepestChildNodes(nd))
            Next
        End If

        Return RetVal
    End Function

再次感谢大家的帮助!!!

于 2013-01-03T17:13:44.180 回答
0

试过递归函数了吗?在 VB.Net 中不确定,但 C# 看起来像

public List<TreeNode> GetDeepestChildren(Treenode MyNode)
{
   if (MyNode.Children.Count > 0)
        GetDeepestChildren(Treenode MyNode);
   else
        return MyNode.Children;
}

这还没有被编译或测试,但是这个想法是存在的,并且应该为任何给定的节点返回最深的孩子。

于 2013-01-03T15:28:15.707 回答
0

我不熟悉 TreeView 控件,但是 TreeNode 的 Level 属性对您有什么好处吗?

http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.level.aspx

如果你知道最深层次,你可以这样做:

C#

private List<TreeNode> GetDeepestChildren(int level)
{
    return (from p in treeView1.Nodes.Cast<TreeNode>() where p.Level == level select p).ToList();
}

VB

Private Function GetDeepestChildren(level As Integer) As List(Of TreeNode)
    Return (From p In treeView1.Nodes.Cast(Of TreeNode)() Where p.Level = levelp).ToList()
End Function

格雷格。

于 2013-01-03T15:41:52.960 回答
0

只是对@dasblinkenlight 答案的轻微修改,所以不要对此表示赞同!

只是个人风格的问题,但我更喜欢这样的递归调用:

IEnumerable<TreeNode> WalkNodes(TreeNode root)
{   
    yield return root;
    var children = root.Nodes.Cast<TreeNode>();
    foreach (var child in children)
    {
        foreach(var subChild in WalkNodes(child))
        {
            yield return subChild;
        }
    }
}

并通过以下方式调用:

foreach (var node in treeView.Nodes.Cast<TreeNode>())
{
    var walkedFrom = WalkNodes(node);
    foreach (var subNode in walkedFrom)
    {
        Console.WriteLine(subNode.Text);
    }
}
于 2013-01-03T16:00:23.097 回答
0

它不是 linq,因为我认为在这种情况下它不应该由 linq 完成,对不起,如果这不是你所要求的,而是这项工作。它不是完全证明的,但如果你得到一棵疯狂的树,至少你不会得到 stackoverflow

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim test1 = GetDeepestChildren(TreeView1.Nodes(0))
    Dim test2 = GetDeepestChildren(TreeView1.Nodes(0).Nodes(0).Nodes(0))
    Dim test3 = GetDeepestChildren(TreeView1.Nodes(0).Nodes(1))
    Dim test4 = GetDeepestChildren(TreeView1.Nodes(0).Nodes(2))
End Sub

Private Function GetDeepestChildren(ByVal node As TreeNode) As List(Of TreeNode)
    Dim deepestList As New List(Of TreeNode)

    If node.Nodes.Count = 0 Then
        Return deepestList
    End If

    Dim nodes As New Stack(Of TreeNode)
    For Each n As TreeNode In node.Nodes
        nodes.Push(n)
    Next

    Dim deepest As Integer = 0
    Do Until nodes.Count = 0
        node = nodes.Pop
        If node.Nodes.Count = 0 Then
            If deepest < node.Level Then
                deepest = node.Level
                deepestList.Clear()
                deepestList.Add(node)
            ElseIf deepest = node.Level Then
                deepestList.Add(node)
            End If
        Else
            For Each n As TreeNode In node.Nodes
                nodes.Push(n)
            Next
        End If
    Loop

    Return deepestList
End Function
于 2013-01-03T16:28:19.970 回答