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.
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.
A rooted binary tree has a root node and every node has at most two children.
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.
In a binary search tree, parent nodes can have a maximum of two children.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With