I have a tree that consists of several objects where each object has a name (string), id (int) and possibly an array of children, that are of the same type. How do I go through the entire tree and print out all of the ids and names?
我有一个由几个对象组成的树,其中每个对象都有一个名称(字符串),id(int)和可能是相同类型的子数组。如何浏览整个树并打印出所有ID和名称?
I'm new to programming and frankly, I'm having trouble wrapping my head around this because I don't know how many levels there are. Right now I'm using a foreach loop to fetch the parent objects directly below rot, put this means I cannot get the children.
我是编程的新手,坦率地说,我无法解决这个问题,因为我不知道有多少级别。现在我正在使用foreach循环直接在rot下面获取父对象,这意味着我无法得到孩子。
2 个解决方案
#1
An algorithm which uses recursion goes like this:
使用递归的算法如下:
printNode(Node node)
{
printTitle(node.title)
foreach (Node child in node.children)
{
printNode(child); //<-- recursive
}
}
Here's a version which also keeps track of how deeply nested the recursion is (i.e. whether we're printing children of the root, grand-children, great-grand-children, etc.):
这是一个版本,它还跟踪递归的嵌套程度(即我们是否打印了root,grand-children,grand-grand-children等的子项):
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
Well, you could always use recursion, but in a "real world" programming scenario, it can lead to bad things if you don't keep track of the depth.
好吧,你总是可以使用递归,但在“真实世界”编程场景中,如果你不跟踪深度,它可能会导致坏事。
Here's an example used for a binary tree: http://www.codeproject.com/KB/recipes/BinarySearchTree.aspx
以下是用于二叉树的示例:http://www.codeproject.com/KB/recipes/BinarySearchTree.aspx
I would google linked lists and other tree structures if you're new to the whole data structure thing. There's a wealth of knowledge to be had.
如果你是整个数据结构的新手,我会google链表和其他树结构。这里有丰富的知识。
#1
An algorithm which uses recursion goes like this:
使用递归的算法如下:
printNode(Node node)
{
printTitle(node.title)
foreach (Node child in node.children)
{
printNode(child); //<-- recursive
}
}
Here's a version which also keeps track of how deeply nested the recursion is (i.e. whether we're printing children of the root, grand-children, great-grand-children, etc.):
这是一个版本,它还跟踪递归的嵌套程度(即我们是否打印了root,grand-children,grand-grand-children等的子项):
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
Well, you could always use recursion, but in a "real world" programming scenario, it can lead to bad things if you don't keep track of the depth.
好吧,你总是可以使用递归,但在“真实世界”编程场景中,如果你不跟踪深度,它可能会导致坏事。
Here's an example used for a binary tree: http://www.codeproject.com/KB/recipes/BinarySearchTree.aspx
以下是用于二叉树的示例:http://www.codeproject.com/KB/recipes/BinarySearchTree.aspx
I would google linked lists and other tree structures if you're new to the whole data structure thing. There's a wealth of knowledge to be had.
如果你是整个数据结构的新手,我会google链表和其他树结构。这里有丰富的知识。