C# TreeNode.Nodes.ContainsKey的算法是什么
我对C# TreeNode.Nodes.ContainsKey的算法是什么,c#,treenode,C#,Treenode,我对TreeNode.Nodes.ContainsKey(字符串键)感到困惑,如果它在其子对象中递归搜索该键,或者只是使用常规的for循环在其子对象中搜索。 如果它递归搜索密钥,是否有只在其子项中搜索的方法?根据,ContainsKey执行以下操作: public virtual bool ContainsKey(string key) { return IsValidIndex(IndexOfKey(key)); } 这种方法可以: public vi
TreeNode.Nodes.ContainsKey(字符串键)
感到困惑,如果它在其子对象中递归搜索该键,或者只是使用常规的for循环在其子对象中搜索。如果它递归搜索密钥,是否有只在其子项中搜索的方法?根据,
ContainsKey
执行以下操作:
public virtual bool ContainsKey(string key) {
return IsValidIndex(IndexOfKey(key));
}
这种方法可以:
public virtual int IndexOfKey(String key) {
// Step 0 - Arg validation
if (string.IsNullOrEmpty(key)){
return -1; // we dont support empty or null keys.
}
// step 1 - check the last cached item
if (IsValidIndex(lastAccessedIndex))
{
if (WindowsFormsUtils.SafeCompareStrings(this[lastAccessedIndex].Name, key, /* ignoreCase = */ true)) {
return lastAccessedIndex;
}
}
// step 2 - search for the item
for (int i = 0; i < this.Count; i ++) {
if (WindowsFormsUtils.SafeCompareStrings(this[i].Name, key, /* ignoreCase = */ true)) {
lastAccessedIndex = i;
return i;
}
}
// step 3 - we didn't find it. Invalidate the last accessed index and return -1.
lastAccessedIndex = -1;
return -1;
}
private bool IsValidIndex(int index) {
return ((index >= 0) && (index < this.Count));
}
public虚拟int索引键(字符串键){
//步骤0-参数验证
if(string.IsNullOrEmpty(key)){
return-1;//我们不支持空键或空键。
}
//步骤1-检查最后一个缓存项
if(IsValidIndex(lastAccessedIndex))
{
if(WindowsFormsUtils.SafeCompareStrings(此[lastAccessedIndex].Name,key,/*ignoreCase=*/true)){
返回lastAccessedIndex;
}
}
//步骤2-搜索项目
for(int i=0;i=0)和&(索引
因此,它似乎只是试图找到该键的索引,如果该索引有效,则意味着该键必须存在。编写代码很简单,可以使用该键获取第一个节点。使用root=true,这样代码就不会检查顶级节点。代码不仅可以用于树状视图的根目录,还可以用于任何其他目录
public KeyValuePair<Boolean, TreeNode> SearchChildren(TreeNode node, string key, Boolean root)
{
if (!root)
{
if(node.Nodes.ContainsKey(key)) return new KeyValuePair<bool, TreeNode>(true, node.Nodes[key]);
}
foreach (TreeNode child in node.Nodes)
{
if (child.Nodes != null)
{
KeyValuePair<Boolean, TreeNode> results = SearchChildren(child, key, false);
if (results.Key)
{
return results;
}
}
}
return new KeyValuePair<bool, TreeNode>(false, null);
}
public KeyValuePair SearchChildren(树节点、字符串键、布尔根)
{
如果(!root)
{
if(node.Nodes.ContainsKey(key))返回新的KeyValuePair(true,node.Nodes[key]);
}
foreach(node.Nodes中的树节点子节点)
{
if(child.Nodes!=null)
{
KeyValuePair结果=搜索子项(子项、密钥、false);
if(results.Key)
{
返回结果;
}
}
}
返回新的KeyValuePair(false,null);
}
TreeNode.Nodes.ContainsKey(字符串键)
仅在子节点中搜索键,该子节点是TreeNode
的直接后代,而不递归检查子节点
TreeNode
的节点
属性属于TreeNodeCollection
类型,它也有一个方法,允许您指定是要递归搜索还是只搜索TreeNode
的直接后代
示例:假设您有一个名为myTreeNode的TreeNode
// search for the key only in direct descendents of myTreeNode
bool keyIsPresent = myTreeNode.Nodes.ContainsKey("someKey");
// value of keyIsPresent will be the same if you specify false
// for the searchAllChildren parameter in Find
bool keyIsPresent = myTreeNode.Nodes.Find("someKey", false).Length > 0;
// value of KeyIsPresent will not necessarily be the same if you
// specify true for the searchAllChildren parameter in Find, which is
// recursive and will search all descendents of myTreeNode
bool keyIsPresent = myTreeNode.Nodes.Find("someKey", true).Length > 0;
因此,Find
方法将为您提供仅搜索直接后代或TreeNode
的所有后代的选项