我相信你正在寻找中序遍历树?除非我误解了某些东西。
http://www.cplusplus.com/forum/beginner/131457/
void Tree:: Inorder(Node* Root)
{
if(Root != NULL)
{
Inorder(Root->Left());
cout << Root->Key() << endl;
Inorder(Root->Right());
}
}
这个小功能使用递归调用打印出他们似乎开始“根”的顺序树的所有节点。我认为,从这个出发点出发,你应该能够相当容易地弄清楚这一切。如果没有,请让我知道(或者如果这不是你要找的东西)。这是一个C++的例子,但它应该仍然帮助你。
编辑:跟踪穿越深度(Traversing a tree of objects in c#)的:
printRoot(Node node)
{
printNode(node, 0);
}
printNode(Node node, int level)
{
printTitle(node.title)
foreach (Node child in node.children)
{
printNode(child, level + 1); //<-- recursive
}
}
编辑2:既然你仍然不确定如何使用这个,我在外面写的代码。
public void InorderTraversal(TreeNode root){
InorderTraversal(root, 0);
}
public void InorderTraversal(TreeNode root, int level){
//this is where you write to excel. You can also use 'level' to decide if you insert in column 1, 2, or 3 just by using a simple if statement like this:
if(level == 0)
sw.WriteLine(node, level.ToString());
foreach (TreeNode child in root.children){ //depending on how your tree is implemented this is probably either going to be root.children or root.next
InorderTraversal(child, level + 1);
}
}
要在代码中使用这样的:
void main(void){
InorderTraversal(treeView1.Nodes);
}
你能举一个简单的输入/输出情况的一个例子,(即给予一定的树,这将是从这个方法您的输出)?这有助于给你的问题一个更明确的答案。 – tjcertified
从下面的Web图像中看到的树形图如下所示。但没有复选框https://www.codeproject.com/Articles/202435/Tri-State-Tree-View – FlaxoIce
树视图看起来像Lets例如我有这棵树 - 从你的一个例子中得到这个。 动物 狗 猫 鱼 淡水 罗奇 鲂 咸水 滑板 SkateChild 灵魂 – FlaxoIce