2011-04-14 102 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; 
    } 
}