2011-04-14 97 views
4

具有樹(在DB邏輯)與項目形式獲取下一個項目在樹上

  1. 列表項
  2. 清單B項
    1. 清單項目C
      1. 清單項目D
  3. 列表E項
  4. 列表項目F
    1. 列表商品G

等(嵌套深度不限),我想下一個節點向下(或向上) ,從任意節點開始。

比方說,List Item D給出我想寫一個函數GetNextNode(),將返回List Item E

我的想法是做一些遞歸的東西,但也許有一個更聰明的方式來處理呢?

我的問題:

你會如何解決這個問題?

EDIT 1:

樹可以用函數訪問,如:

  • GetParentNode()
  • GetChildrenNodes()
  • GetNextSiblingNode()

所以它類似於例如ËWindows窗體TreeView

(不,這不是功課,我目前38年沒有孩子,所以既不我也不要他們必須做的功課;-)

+1

你問的是如何將它存儲在數據庫中,你的項目是否已經存在於你的窗體的TreeView中,或者一般如何創建這樣的東西? – Jaapjan 2011-04-14 09:31:43

+1

你可以多說一下如何表示樹的結構? – 2011-04-14 09:32:12

+1

在樹中迭代有很多種不同的方式 - 樹「遍歷」是一個複雜的主題。 > http://en.wikipedia.org/wiki/Tree_traversal <值得注意的是,樹結構和遞歸併行,一般來說,如果沒有其他的,你就不會得到一個。 – MattDavey 2011-04-14 10:13:24

回答

2

我不得不這樣做幾次。從內存:

public Node GetBelowNode() 
{ 
    if (GetChildrenNodes().count > 0) 
     return GetChildrenNodes()[0]; 
    else 
     if (GetNextSiblingNode() != null) 
      return GetNextSiblingNode(); 
     else 
     { 
      Node curr = this; 
      Node parent; 
      while (true) 
      { 
       parent = curr.GetParentNode(); 
       if (parent == null) 
        return null; 
       else 
       { 
        if (parent.GetNextSiblingNode() != null) 
         return parent.GetNextSiblingNode(); 
        else 
         curr = parent; 
       } 
      } 
     } 
} 
+0

看起來不錯,謝謝! – 2011-04-14 09:54:47

0

試試這個可能:

 TreeNode currentNode = treeView1.SelectedNode; 
     treeView1.selectedNode = currentNode.NextNode; 
+0

這是爲'TreeView'樹(我只有一個邏輯內存樹)。另外,你的函數似乎只適用於兄弟姐妹(http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.nextnode.aspx)。 – 2011-04-14 09:49:12

1

private string getNext(TreeNode node) 
     { 
      if (node.FirstNode != null) 
      { 
       return node.FirstNode.Name; 
      } 
      else 
      { 
       if (node.NextNode != null) 
       { 
        return node.NextNode.Name; 
       } 
       else if (node.Parent.NextNode != null) 
       { 
        return node.Parent.NextNode.Name; 
       } 
      } 

      return ""; 
     } 

您可以通過遞歸或...最壞的xD

我認爲只有3個基本的情況下處理這個問題

這並不適用於所有情況。你也應該搜索父節點的下一個節點。感謝文森特Vancalbergh的評論;-)

+0

嵌套深度應該是任意的。 – 2011-04-14 09:47:18

+1

這適用於Windows窗體中的TreeView。 – zapico 2011-04-14 09:54:30

+1

FirstNode是第一個子節點,NextNode是當前節點之後的第一個「兄弟」,Parent是父節點;-) – zapico 2011-04-14 09:55:30

0

由於我得到了一個很好的答覆「下」部分,我會加入我自己的「向上」的一部分。也許這是你的一些幫助; up部分類似於:

  1. 獲取以前的兄弟姐妹。
  2. 如果有以前的兄弟姐妹,得到這個兄弟姐妹的最深的子節點。
  3. 如果有沒有以前的兄弟姐妹,直接獲得父母。

爲了得到最深的兄弟姐妹(2),我使用下面的代碼:

function getDeepestChild(page) 
    dim result 
    set result = nothing 

    dim childPages 
    set childPages = page.ChildPages 

    if childPages.Count>0 then 
     dim p 
     set p = childPages(childPages.Count-1) 
     ' recurse. 
     set result = getDeepestChild(p) 
    else 
     set result = page 
    end if 

    set getDeepestChild = result 
end function 

(是的,我知道這是VBScript中,我需要它其實在這門語言)

1
public Party Next { 
    get { 
     if (this.children.Count > 0) return this.children[0]; 

     Party target = this; 
     do { 
      if (target.NextSibling != null) return target.NextSibling; 
     } while ((target = target.Parent) != null); 

     return null; 
    } 
} 

public Party Previous { 
    get { 
     if (Parent != null && Parent.children.Count > 0 && this == Parent.children[0]) return Parent; 

     Party target = this; 

     do { 
      if (target.PreviousSibling != null) { target = target.PreviousSibling; break; } 
     } while ((target = target.Parent) != null); 

     if (target != null) { 
      while (target.children.Count > 0) { 
       target = target.children[target.children.Count - 1]; 
      } 
     } 

     return target; 
    } 
}