树遍历算法

发布于 2024-10-14 14:43:37 字数 863 浏览 4 评论 0原文

更新:
我找到了更多我想要实现的示例: 管理层次结构MySQL 中的数据。我想在 JavaScript 中做到这一点,因为我正在构建一个应用程序,它接受分层结构中的评论,更具体地说是 reddit.com。如果您的 Chrome Web 浏览器上有 Pretty JSON 扩展,请转到 reddit 并单击线程评论,然后将 .json 添加到 url 以查看我正在解析的内容。
我得到了 JSON 数据,它只是解析注释并添加适当的 HTML 以显示其嵌套。
解决方案的想法?


老问题:
我正在开发一个程序,在编写代码之前我需要弄清楚逻辑。 我正在接受树格式的数据,但每个父节点可能有多个子节点,并且我似乎可以找到的唯一树的数据是具有权重的树或最多每个节点有两个子节点的树。所以我试图找出评估树的每个节点的算法,如下所示:

startingParent[15] // [# of children]
    child1[0]
    child2[5]
       child2ch1[4]
       ...
       child2ch5[7]
    child3[32]
    ...
    child15[4]

现在,当我尝试写出我的算法如何工作时,我最终会编写嵌套的 for/while 循环,但最终会为每个节点编写一个循环树的高度级别,对于动态数据和未知高度的树(每个节点的子节点数量未知),这是不起作用的。我知道在某个时候我学会了如何遍历这样的树,但现在我完全忘记了它。有人知道这是如何在循环方面完成的吗?

Update:
I found more of an example of what I'm trying to pull off: Managing Hierarchical Data in MySQL. I want to do that but in JavaScript because I am building an app that takes in comments that are in a hierarchical structure, to be more specific reddit.com. If you have the Pretty JSON extension on your chrome web browser go to reddit and click on a threads comments and then add .json to the url to see what I am parsing.
I get the JSON data just fine, its just parsing through the comments and adding the appropriate HTML to show that its nested.
Ideas for solutions?


OLD question:
I am working on a program and I have come to a part that I need to figure out the logic before I write the code.
I am taking in data that is in a tree format but with the possibility of several children for each parent node and the only tree's I can seem to find data on are tree's with weights or tree's where at most each node has two child nodes. So I'm trying to figure out the algorithm to evaluate each node of a tree like this:

startingParent[15] // [# of children]
    child1[0]
    child2[5]
       child2ch1[4]
       ...
       child2ch5[7]
    child3[32]
    ...
    child15[4]

Now when I try to write out how my algorithm would work I end up writing nested for/while loops but I end up writing a loop for each level of the height of the tree which for dynamic data and tree's of unknown height with unknown number of children per node this doesn't work. I know that at some point I learned how to traverse a tree like this but its completely escaping me right now. Anyone know how this is done in terms of loops?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风流物 2024-10-21 14:43:37

如果您不打算使用递归,则需要一个辅助数据结构。队列将为您提供广度优先遍历,而堆栈将为您提供深度优先遍历。无论哪种方式,它看起来大致如下:

structure <- new stack (or queue)
push root onto structure
while structure is not empty
  node <- pop top off of structure
  visit(node)
  for each child of node
     push child onto structure
loop

维基百科参考

If you're not going to use recursion, you need an auxiliary data structure. A queue will give you a breadth-first traversal, whereas a stack will give you a depth-first traversal. Either way it looks roughly like this:

structure <- new stack (or queue)
push root onto structure
while structure is not empty
  node <- pop top off of structure
  visit(node)
  for each child of node
     push child onto structure
loop

Wikipedia References

情归归情 2024-10-21 14:43:37

使用递归,而不是循环。
广度优先搜索
深度优先搜索
这些应该可以帮助您开始实现您想要完成的目标

Use recursion, not loops.
Breadth first search
Depth first search
Those should help you get started with what you're trying to accomplish

黑白记忆 2024-10-21 14:43:37

只需使用递归即可

def travel(node):
    for child in node.childs:
        # Do something
        travel(child)

Just use recursion like

def travel(node):
    for child in node.childs:
        # Do something
        travel(child)
┾廆蒐ゝ 2024-10-21 14:43:37

大多数树遍历的最简单代码通常是递归的。对于像您这样的多路树,通常最简单的方法是使用一个循环来查看指向子节点的每个指针,并为所有子节点调用该节点作为参数自身。

The simplest code for most tree traversal is usually recursive. For a multiway tree like yours, it's usually easiest to have a loop that looks at each pointer to a child, and calls itself with that node as the argument, for all the child nodes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文