如果TreeNode.Nodes.ContainsKey(string key)
如果它的子對象在鍵上遞歸搜索,或者只是使用常規for循環搜索子對象,我會感到困惑。
如果它遞歸搜索關鍵,是否有一種方法只在其子中搜索?什麼是TreeNode.Nodes.ContainsKey的算法
2
A
回答
2
按照Reference Source,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));
}
所以看起來它只是試圖找到一個關鍵的指標,如果它是那麼這就意味着鑰匙必須存在。
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
,且不進行遞歸查詢子節點。
的TreeNode
的Nodes
屬性,它是TreeNodeCollection
型的,也有Find(string key, bool searchAllChildren)
方法,它允許您指定是否要遞歸搜索或只是搜索TreeNode
的直接後裔。
例子:假設你有一個叫myTreeNode樹節點...
// 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
的所有後代的選擇。
相關問題
- 1. 什麼是算法
- 2. 這是什麼算法?
- 3. 什麼是McNaughton-Yamada算法?
- 4. 這是什麼算法?
- 5. 什麼是s2k算法?
- 6. 什麼是cooks-kim算法
- 7. 這是什麼算法
- 8. 什麼是單遍算法
- 9. 什麼是Excel Days360算法?
- 10. 什麼是「make -jX」算法
- 11. 減法算法的意思是什麼?
- 12. 什麼是單程算法,是我的算法?
- 13. 自然算法顯示計算答案的算法是什麼
- 14. 什麼是基於規則的算法?
- 15. 什麼是好的(半)異步算法?
- 16. Random.next()背後的算法是什麼?
- 17. search()函數的算法是什麼?
- 18. Robocopy背後的算法是什麼?
- 19. 什麼是好的重疊組算法?
- 20. 什麼是洗牌最好的算法?
- 21. 什麼是好的輪詢算法
- 22. 什麼是Linux diff -y的算法?
- 23. 這個算法的上界是什麼?
- 24. 什麼是最重要的算法?
- 25. 什麼是java互補的算法
- 26. 展位算法的本質是什麼?
- 27. 這是什麼樣的排序算法?
- 28. 什麼是Dijkstra算法中的INT_MAX?
- 29. 「失明」的算法是什麼?
- 30. 這個算法的名字是什麼?
謝謝,這是非常有幫助:) –