Question: I have the following two methods which loop either a TreeView or TreeNode which I want to combine into a single method. Instead of passing the TreeView/TreeNode I could pass an Object and check the type but wanted to know if there was better way?

```
private TreeNode SearchNode(string nodetext, TreeNode node)
{
    foreach (TreeNode nd in node.Nodes)
    {
        if (nd.Text == nodetext)
        {
            return nd;
        }
    }
    return null;
}

private TreeNode SearchParentNode(string nodetext, TreeView trv)
{
    foreach (TreeNode node in trv.Nodes)
    {
        if (node.Text == nodetext)
        {
            return node;
        }
    }
    return null;
}

```

Answer: 
Data structures that resemble a tree are best searched using a recursive algorithm. Which could look like:

```
    public static TreeNode SearchNode(string tofind, TreeNodeCollection nodes) {
        foreach (TreeNode node in nodes) {
            if (node.Text == tofind) return node;
            var nested = SearchNode(tofind, node.Nodes);
            if (nested != null) return nested;
        }
        return null;
    }

```

Sample usage:

```
    var node = SearchNode("foo", treeView1.Nodes);
    if (node != null) {
        node.Expand();
        node.EnsureVisible();
    }

```

Note how this works for *any* TreeView, not just one that's limited to a single parent-child relationship. It is a depth-first search, locating the first string in the order the user sees them on the screen.
```
private TreeNode SearchNode(string nodetext, object obj)
{
    if(obj.GetType() Is TreeView)
    {
          t = (TreeView)obj;
          foreach (TreeView tv in obj.Nodes)
          {
             if (tv.Text == nodetext)
             {
                 return nd;
             }
          }
    }
    else
    {
          n = (TreeNode)obj;
          foreach (TreeNode nd in obj.Nodes)
          {
              if (nd.Text == nodetext)
              {
                 return nd;
              }
          }
    }
    return null;
}

```