DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: nodes in a tree

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. 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....

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. 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....

>
>
>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
•

 FAQ Latest Articles Java .NET XML Database Enterprise