Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find child nodes at root node [TreeView]

 ROOT
      A 
        B
          C 
            D 
              E
        T
      F
      G
        X

I want to find E Node's parent nodes(it is number 5). Then, I'll save node. If number is smaller 5. I'm using TreeView in Asp.net control.

like image 707
ozkank Avatar asked Sep 22 '10 09:09

ozkank


People also ask

How many root nodes can a TreeView control have?

A typical tree structure has only one root node; however, you can add multiple root nodes to the TreeView control. The Nodes property can also be used to manage the root nodes in the tree programmatically. You can add, insert, remove, and retrieve TreeNode objects from the collection.

How many children does the root node have?

A rooted binary tree has a root node and every node has at most two children.

How do I know if TreeView node is selected?

SelectedNode; if ( tn == null ) Console. WriteLine("No tree node selected."); else Console. WriteLine("Selected tree node {0}.", tn.Name ); You can compare the returned TreeNode reference to the TreeNode you're looking for, and so check if it's currently selected.

How many child nodes can a parent node have?

In a binary search tree, parent nodes can have a maximum of two children.


1 Answers

I would suggest using recursive iterations.

private TreeNode FindNode(TreeView tvSelection, string matchText) 
{ 
    foreach (TreeNode node in tvSelection.Nodes) 
    { 
        if (node.Tag.ToString() == matchText) 
        {
            return node; 
        }
        else 
        { 
            TreeNode nodeChild = FindChildNode (node, matchText); 
            if (nodeChild != null) return nodeChild; 
        } 
    } 
    return (TreeNode)null; 
}

You can utilize this logic to determine many things about you node and this structure also allows you to expand what you can do with the node and the criteria you wish to search for. You can edit my example to fit your own needs.

Thus, with this example you could pass in E and expect to have the node E returned then simply if the parent property of the node returned would be the parent you are after.

tn treenode = FindNode(myTreeview, "E")

tn.parent is the value you are after.

like image 52
Michael Eakins Avatar answered Nov 15 '22 05:11

Michael Eakins