DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: nodes in a tree

  1. #1
    Deborah B. Guest

    nodes in a tree


    Is there an easy way to iterate thru a tree to tell me how deep the node levels
    go? In other words, (trying to make it more clear) iterate thru the nodes
    and tell me if the tree has three sublevels, or two, etc. I can seem to
    think the logic thru like i need to be doing....

  2. #2
    Jeremiah Guest

    Re: nodes in a tree


    "Deborah B." <deborahb@NOSPAM.tampabay.rr.com> wrote:
    >
    >Is there an easy way to iterate thru a tree to tell me how deep the node

    levels
    >go? In other words, (trying to make it more clear) iterate thru the nodes
    >and tell me if the tree has three sublevels, or two, etc. I can seem to
    >think the logic thru like i need to be doing....


    How about something like this?

    Public Function GetNodeDepth() As Integer
    Dim nTemp As Node
    Dim nDeepestNode As Node
    Dim intMaxDepth As Integer
    Dim intMaxTemp As Integer

    For Each nTemp In tvTreeView.Nodes
    intMaxTemp = NodeDepth(nTemp) + 1
    If intMaxTemp > intMaxDepth Then
    intMaxDepth = intMaxTemp
    Set nDeepestNode = nTemp
    End If
    Next
    MsgBox "Deepest Node: " & nDeepestNode.Key & " Depth = " & intMaxDepth
    Set nDeepestNode = Nothing
    Set nTemp = Nothing
    End Function

    Private Function NodeDepth(ByRef nParent As Node) As Integer
    If Not nParent.Parent Is Nothing Then
    ' Node has parent, count it
    NodeDepth = NodeDepth(nParent.Parent) + 1
    End If
    End Function


  3. #3
    Deborah B. Guest

    Re: nodes in a tree


    works beautifully, exactly what I was looking for. I thank you immensely!!


    "Jeremiah" <sabjkb@speakeasy.org> wrote:
    >
    >"Deborah B." <deborahb@NOSPAM.tampabay.rr.com> wrote:
    >>
    >>Is there an easy way to iterate thru a tree to tell me how deep the node

    >levels
    >>go? In other words, (trying to make it more clear) iterate thru the nodes
    >>and tell me if the tree has three sublevels, or two, etc. I can seem to
    >>think the logic thru like i need to be doing....

    >
    >How about something like this?
    >
    >Public Function GetNodeDepth() As Integer
    > Dim nTemp As Node
    > Dim nDeepestNode As Node
    > Dim intMaxDepth As Integer
    > Dim intMaxTemp As Integer
    >
    > For Each nTemp In tvTreeView.Nodes
    > intMaxTemp = NodeDepth(nTemp) + 1
    > If intMaxTemp > intMaxDepth Then
    > intMaxDepth = intMaxTemp
    > Set nDeepestNode = nTemp
    > End If
    > Next
    > MsgBox "Deepest Node: " & nDeepestNode.Key & " Depth = " & intMaxDepth
    > Set nDeepestNode = Nothing
    > Set nTemp = Nothing
    >End Function
    >
    >Private Function NodeDepth(ByRef nParent As Node) As Integer
    > If Not nParent.Parent Is Nothing Then
    > ' Node has parent, count it
    > NodeDepth = NodeDepth(nParent.Parent) + 1
    > End If
    >End Function
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center