在表单中有一个树(在DB中是逻辑的)
- 清单项目A.
- 清单项目B.
- 清单项目C.
- 清单项目D.
- 清单项目E.
- 清单项目F.
- 清单项目G.
等等(嵌套深度不受限制),我想从任意节点开始向下(或向上)获取下一个节点.
让我们说,List Item D我想写一个GetNextNode()会返回的函数List Item E.
我的想法是做一些递归的东西,但也许有一个更聪明的方法来处理这个?
我的问题:
你怎么解决这个问题?
编辑1:
可以使用以下函数访问树:
GetParentNode()GetChildrenNodes()GetNextSiblingNode()所以它类似于e Windows Forms TreeView.
我不得不这样做几次.从记忆里:
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)
| 归档时间: |
|
| 查看次数: |
1752 次 |
| 最近记录: |