2017-06-25 18 views
2

私はTreeNode.Nodes.ContainsKey(string key)については混乱しています。子ノードでキーを再帰的に検索したり、通常のforループのみを使用して子ノードを検索すると混乱します。
再帰的にキーを検索する場合、その子のみを検索する方法はありますか? Reference SourceによるとTreeNode.Nodes.ContainsKeyのアルゴリズムは何ですか

答えて

2

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

だから、それだけでキーのインデックスを見つけようと表示され、それはだ場合有効であれば、キーが存在しなければならないことを意味します。

+0

ありがとう、それはとても役に立ちます:) –

1

コードは、キーで最初のノードを取得するのが簡単です。コードがトップレベルのノードをチェックしないように、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); 
     } 
1

TreeNode.Nodes.ContainsKey(string key)TreeNodeの直接の子孫である子ノードでkeyを検索し、再帰的に子ノードをチェックしません。タイプTreeNodeCollectionであるTreeNode

Nodesプロパティは、また、あなたが再帰的に検索またはちょうどTreeNodeの直接の子孫を検索するかどうかを指定することができますFind(string key, bool searchAllChildren)方法があります。

例:

// 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のすべての子孫を検索するオプションを提供します...あなたはmyTreeNodeと呼ばれるツリーノードがあるとします。

関連する問題