获取树中的下一个项目

Uwe*_*eim 6 c# tree recursion

在表单中有一个树(在DB中是逻辑的)

  1. 清单项目A.
  2. 清单项目B.
    1. 清单项目C.
      1. 清单项目D.
  3. 清单项目E.
  4. 清单项目F.
    1. 清单项目G.

等等(嵌套深度不受限制),我想从任意节点开始向下(或向上)获取下一个节点.

让我们说,List Item D我想写一个GetNextNode()会返回的函数List Item E.

我的想法是做一些递归的东西,但也许有一个更聪明的方法来处理这个?

我的问题:

你怎么解决这个问题?

编辑1:

可以使用以下函数访问树:

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

所以它类似于e Windows Forms TreeView.

Vin*_*rgh 5

我不得不这样做几次.从记忆里:

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;
                }
            }
        }
}
Run Code Online (Sandbox Code Playgroud)