?
Solved

Bad Performance with Recursive Treeview Functions

Posted on 2004-09-17
4
Medium Priority
?
373 Views
Last Modified: 2008-01-09
Hi,

I’ve written some recursive treeview browsing functions. The sample below shows a function, which only searches and highlights a node.

But the performance is very, very bad. My Treeview has about 2000 nodes. Any suggestions?

Many thanks
Dominic Birrer

Here are the two subs (first the initial sub, then the recursive sub):

    '*** Loop sub through all rootnodes
  Public Sub SearchWAs(ByRef MyTrv As firstTreeView.Cls_firstTreeView, ByVal PK_WA As Integer)

        Dim RootNode As New firstTreeView.Cls_firstTreeNode()

        '*** Loop sub through all rootnodes
        For Each RootNode In MyTrv.Nodes

            '*** if node found
            If RootNode.PK = PK_WA And RootNode.Key1 = "WA" Then
                RootNode.Expand()
                RootNode.Key5 = "1"
                RootNode.BackColor = Color.Tomato
            End If

            '*** start the recursive sub
            SearchWAs_Rec(RootNode, PK_WA)
        Next

    End Sub

    '*** recursive Loop sub through all childnodes
    Public Sub SearchWAs_Rec(ByVal ParentNode As firstTreeView.Cls_firstTreeNode, ByVal PK_WA As Integer)

        Dim ChildNode As New firstTreeView.Cls_firstTreeNode()

        '*** recursive Loop sub through all childnodes
        For Each ChildNode In ParentNode.Nodes

            '*** if node found
            If ChildNode.PK = PK_WA And ChildNode.Key1 = "WA" Then
                ChildNode.BackColor = Color.Tomato
                ChildNode.Key5 = "1"
                '*** starts an other recursive sub to expand all nodes from the actual node up to the root node (this sub is build similar)
                ExpandParentNodes_Rec(ChildNode)
            End If

            '*** start itself with actual node as parentnode
            SearchWAs_Rec(ChildNode, PK_WA)

        Next

    End Sub
0
Comment
Question by:fs25053
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
tzxie2000 earned 1000 total points
ID: 12083532
please test whether Enum is more quick:

Public Sub SearchWAs(Byval searchtreeview as treeview)
   IEnumerator myEnumerator = searchtreeview.nodes.GetEnumerator()
   while(myEnumerator.MoveNext())  
       dim curnode as treenode =(TreeNode)myEnumerator.Current
       if (curnode.Text.equal("WA")) then
                curnode.Expand()
                curnode.Key5 = "1"
                curnode.BackColor = Color.Tomato
       end if
   end while
 End Sub


0
 
LVL 6

Expert Comment

by:KGreg
ID: 12084585
This simplifies it.  I'm sure you can modify it to accomodate whatever treeview control you're using.  The first call you make should be to nodecollection(myTreeview.Nodes)


    Private Sub nodecollection(ByRef nodes As TreeNodeCollection)
        If Not nodes Is Nothing Then
            Dim n As TreeNode

            For Each n In nodes
                singlenode(n)
            Next

        End If
    End Sub

    Private Sub expandancestors(ByRef n As TreeNode)
        n.Expand()

        If Not n.Parent Is Nothing Then
            expandancestors(n)
        End If
    End Sub

    Private Sub singlenode(ByRef n As TreeNode)

        If n.Tag = "WA" Then
            n.Expand()
            n.BackColor = Color.Tomato
        End If

        If Not n.Parent Is Nothing Then
            ' This is a child node expand ancestors
            expandancestors(n.Parent)
        End If

        nodecollection(n.Nodes)
    End Sub

KGreg
0
 

Author Comment

by:fs25053
ID: 12084641
Many thanks ! This works very well!! Extremely increased performance...yeeeehaaaw..

Cheers,
Dominic

BTW - Here's the VB.Net Translation (if somebody else has the same problem):

    Public Sub SearchWAs(ByRef MyTrv As firstTreeView.Cls_firstTreeView, ByVal PK_WA As Integer)

        Dim myEnumerator As IEnumerator = MyTrv.Nodes.GetEnumerator

        ClearNodeSettings(MyTrv)

        While (myEnumerator.MoveNext())
            Dim RootNode As firstTreeView.Cls_firstTreeNode = CType(myEnumerator.Current, firstTreeView.Cls_firstTreeNode)

            If RootNode.PK = PK_WA And RootNode.Key1 = "WA" Then
                RootNode.Expand()
                RootNode.Key5 = "1"
                RootNode.BackColor = Color.Tomato
            End If

            SearchWAs_Rec(RootNode, PK_WA)

        End While

    End Sub

   Public Sub SearchWAs_Rec(ByVal ParentNode As firstTreeView.Cls_firstTreeNode, ByVal PK_WA As Integer)

        Dim myEnumerator As IEnumerator = ParentNode.Nodes.GetEnumerator

        While (myEnumerator.MoveNext())
            Dim ChildNode As firstTreeView.Cls_firstTreeNode = CType(myEnumerator.Current, firstTreeView.Cls_firstTreeNode)

            If ChildNode.PK = PK_WA And ChildNode.Key1 = "WA" Then
                ChildNode.BackColor = Color.Tomato
                ChildNode.Key5 = "1"

                ExpandParentNodes_Rec(ChildNode)

            End If

           SearchWAs_Rec(ChildNode, PK_WA)

        End While

    End Sub
0
 
LVL 6

Expert Comment

by:KGreg
ID: 12084731
I'd still recommend cleaning up the code by combining the enumerator with the code i've written.. it could speed it up more.

    Public Sub searchNodes(ByRef n As TreeNodeCollection, ByVal PK_WA As Integer)

        Dim myEnumerator As IEnumerator = n.GetEnumerator

        While (myEnumerator.MoveNext())
            singlenode(CType(myEnumerator.Current, firstTreeView.Cls_firstTreeNode))
        End While

    End Sub

    Public Sub singlenode(ByRef n As firstTreeView.Cls_firstTreeNode, ByVal PK_WA As Integer)
        If n.PK = PK_WA And n.Key1 = "WA" Then
            n.BackColor = Color.Tomato
            n.Key5 = "1"

            If Not IsNothing(n.Parent) Then ExpandParentNodes_Rec(n)
        End If

        searchNodes(n.Nodes)
    End Sub

KGreg
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question