0

I have the following code that compares the node.Texto each both given node sets and then return one if they are equal otherwise zero. But my problem is that it just compare the first children because of nodes2.Nodes[ii] hence I know that it will not go forward more.

As I know if it was TreeNodeCollection it was easy to do recursive for each node and sub-node using foreach loop.

But here how I could change the code to the recursive version? thanks in advance!

public int Compare_ChildNodes(TreeNode nodes1, TreeNode nodes2) { int length_children1 = nodes1.Nodes.Count; int length_children2 = nodes2.Nodes.Count; int result_int = 1; if (length_children1 != length_children2) { result_int = 0; } else { for (int ii = 0; ii < length_children1; ii++) { if (nodes1.Nodes[ii].Text.Equals(nodes2.Nodes[ii].Text)) { int ret = Compare_ChildNodes(nodes1.Nodes[ii], nodes2.Nodes[ii]); result_int = ret; } else { result_int = 0; } } } return result_int; } 

1 Answer 1

1

I can't see any problems here. Calling Compare_ChildNodes with nodes1.Nodes[ii] and nodes2.Nodes[ii] does exactly the recursion you want.

I'd just suggest a little optimization ("early out") for your code:

public int Compare_ChildNodes(TreeNode nodes1, TreeNode nodes2) { int length_children1 = nodes1.Nodes.Count; int length_children2 = nodes2.Nodes.Count; int result_int = 1; if (!nodes1.Text.Equals(nodes2.Text)) return 0; // not equal if (length_children1 != length_children2) return 0; // not equal return nodes1.Nodes.OfType<TreeNode>.Select((node, idx) => Compare_ChildNodes(node, nodes2.Nodes[idx]).Any(result => result == 0) ? 0 : 1; } 

I changed the comparison for the node's text to another recursion level so you can recurse using linq.

The linq Any method checks if any of the comparisons (in the Select method) returns 0 indicating that a child node collection is not equal.

The Select is called for each TreeNode in node1.Nodes and with it's index in that collection. So you can use this index to get the matching node from node2.Nodes and call Compare_ChildNodes for these two.

As soon as you find unequal (child-)nodes, you can return 0 and don't need to continue comparing the other nodes.

If you cannot use the linq statements (for Framework reasons or others), you can still use your for loop:

for (int idx = 0; idx < length_children1; idx++) if (Compare_ChildNodes(nodes1.Nodes[idx], nodes2.Nodes[idx]) == 0) return 0; return 1; 
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! but I have this error: Error 1 'System.Linq.ParallelEnumerable.OfType<TResult>(System.Linq.ParallelQuery)' is a 'method', which is not valid in the given context
@SaberJalilzadeh this should actually be System.Linq.Enumerable.OfType<TResult>. Can you check your using statements?
@SaberJalilzadeh added a non-linq version to my answer.
I have these using statements: using System.Linq; and using System.Xml.Linq;
@SaberJalilzadeh This is an interesting problem...I don't know why your compiler does not choose Enumerable.OfType...works fine here. Your TreeNodes are System.Windows.Forms.TreeNode?
|