TreeNode.Nodes.ContainsKey的算法是什么

What is the algorithm of TreeNode.Nodes.ContainsKey

我很困惑 TreeNode.Nodes.ContainsKey(string key) 它是在其子项中递归搜索密钥,还是仅使用常规 for 循环在其子项中搜索。
如果它递归搜索密钥,是否有一种方法可以只在它的孩子中搜索?

根据 Reference SourceContainsKey 执行以下操作:

    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));
    }

所以看起来它只是试图找到密钥的索引,如果它是有效的,那么这意味着密钥必须存在。

代码很容易编写,以获取第一个带有密钥的节点。使用 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);
        }

TreeNode.Nodes.ContainsKey(string key)只在TreeNode的直系子节点中搜索key,不递归检查子节点。

TreeNodeNodes属性,属于TreeNodeCollection类型,也有一个Find(string key, bool searchAllChildren)方法,可以指定是否或您不想递归搜索或只搜索 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.

的所有后代的选项