开发者

Comparing trees in a treeview

I want to be able to iterate through a tree and compare nodes one tree to the nodes in another tree of the same format.

EX: Ther开发者_JS百科e are five categories.

1 - 5. Are all static and identical between both trees.

In 1. All static values. So i need to just compare the values of the nodes there.

In 2. This is comes from a KVP object and so these can be different in terms of number of nodes and their single children.

In 3. Same as 2 but there are 5 children

In 4-5 are the same as 1.

I was thinking of looping in the trees and having a different for loop for each category and checking the nodes contain the same values.

The dynamic one for 2 and 3 I would do something of the same, but check the size first then loop through if the size is the same.

If i find a difference I would change that nodes back color.

Is this the best method for me?


I don't think you have much of a choice but to do different comparison algorithms based on the data that in the tree since it seems that your nodes are symantically different depending on where they are in the tree.


Since I knew the depth...i did this:

 For h As Integer = 0 To tree1.Nodes(0).Nodes.Count - 1
        For i As Integer = 0 To tree1.Nodes(0).Nodes(h).Nodes.Count - 1
            For j As Integer = 0 To tree1.Nodes(0).Nodes(h).Nodes(i).Nodes.Count - 1

                If tree1.Nodes(0).Nodes(h).Nodes(i).Text <> _
                        tree2.Nodes(0).Nodes(h).Nodes(i).Text Then
                    tree2.Nodes(0).Nodes(h).Nodes(i).BackColor = Color.Red
                    tree2.Nodes(0).Nodes(h).Nodes(i).Nodes(j).Expand()
                    tree2.Nodes(0).Nodes(h).Nodes(i).Expand()
                    tree2.Nodes(0).Nodes(h).Expand()
                    tree2.Nodes(0).Expand()

                    tree1.Nodes(0).Nodes(h).Nodes(i).BackColor = Color.Red
                    tree1.Nodes(0).Nodes(h).Nodes(i).Nodes(j).Expand()
                    tree1.Nodes(0).Nodes(h).Nodes(i).Expand()
                    tree1.Nodes(0).Nodes(h).Expand()
                    tree1.Nodes(0).Expand()


                ElseIf tree1.Nodes(0).Nodes(h).Nodes(i).Nodes(j).Text <> _
                    tree2.Nodes(0).Nodes(h).Nodes(i).Nodes(j).Text Then
                    tree2.Nodes(0).Nodes(h).Nodes(i).Nodes(j).BackColor = Color.Red
                    tree2.Nodes(0).Nodes(h).Nodes(i).BackColor = Color.Red
                    tree2.Nodes(0).Nodes(h).Nodes(i).Nodes(j).Expand()
                    tree2.Nodes(0).Nodes(h).Nodes(i).Expand()
                    tree2.Nodes(0).Nodes(h).Expand()
                    tree2.Nodes(0).Expand()

                    tree1.Nodes(0).Nodes(h).Nodes(i).Nodes(j).BackColor = Color.Red
                    tree1.Nodes(0).Nodes(h).Nodes(i).BackColor = Color.Red
                    tree1.Nodes(0).Nodes(h).Nodes(i).Nodes(j).Expand()
                    tree1.Nodes(0).Nodes(h).Nodes(i).Expand()
                    tree1.Nodes(0).Nodes(h).Expand()
                    tree1.Nodes(0).Expand()
                End If
            Next

        Next
    Next
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜