简体   繁体   English

父级数组n-ary树的逐级遍历?

[英]Level by level traversal of parent array n-ary tree?

Given an n-ary tree stored in a parent array, with the children stored in an array of pointers to arrays where the first value is the number of children: 给定存储在父数组中的n-ary树,子节点存储在指向数组的指针数组中,其中第一个值是子节点数:

(childArray[2][0] shows that node 2 has 2 children, childArray[2][1] shows that its first child is 5, etc.) (childArray [2] [0]显示节点2有2个子节点,childArray [2] [1]显示它的第一个子节点是5,等等)

parentArray = {3, 0, 3, -1, 3, 2, 2};
childArray = {{1, 1}, {0}, {2, 5, 6}, {3, 0, 2, 4}, {0}, {0}, {0}};

produces a tree that looks like this: 生成一个如下所示的树:

  3
 /|\
0 2 4
| |\
1 5 6

Using a queue, how can I output the tree level by level like so: 使用队列,我如何按级别输出树级,如下所示:

Level 1: 3 1级:3级

Level 2: 0, 2, 4 等级2:0,2,4

Level 3: 1, 5, 6 等级3:1,5,6

Levels 1 and 2 are easy, because level 1 is just the root and level 2 is just its children, but after that I can't figure out how to get it to get the children of the children. 1级和2级很容易,因为1级只是它的根,2级只是它的孩子,但在那之后我无法弄清楚如何让它来接收孩子的孩子。

One way of doing so would be using a queue data structure. 这样做的一种方法是使用队列数据结构。

Start with some queue q , and place in the index of the (unique) item whose parent is -1. 从一些队列q开始,并放在父级为-1的(唯一)项的索引中。 Now, at each step, until q is empty, 现在,在每一步,直到q为空,

  • Perform v <- pop(q) (popping the head) 执行v < - pop(q) (弹出头部)
  • Print out v 打印出v
  • For each child w of v , do push(q, v) (pushing ot the tail) 对于每一个孩子W¯¯ ,做俯卧撑(Q,V)(OT推尾部)

For example, here are the first steps for your case: 例如,以下是您案例的第一步:

  • Initially, q = [3] (3 is the index of the item whose parent is -1). 最初, q = [3] (3是其父项为-1的项的索引)。
  • We pop q , print out 3, and push 0, 2, and 4, so q = [0, 2, 4] . 我们弹出q ,打印3,然后按0,2和4,所以q = [0,2,4]
  • Now we pop q , print out 0, and push 1, so q = [2, 4, 1] . 现在我们弹出q ,打印出0,然后按1,所以q = [2,4,1]

Almost by definition, since q is popped from the front and added to the back, the nodes will be processed level by level. 几乎按照定义,由于q从前面弹出并添加到后面,因此将逐级处理节点。

The complexity is linear in the number of nodes. 复杂度在节点数量上是线性的。

You will have to perform a BFS (Breadth First Search) on the tree, while maintaining the number of nodes pushed into the next level. 您必须在树上执行BFS (广度优先搜索),同时保持推入下一级别的节点数。 Outline: 大纲:

q.push(root); nodesInCurrentLevel = 1; nodesInNextLevel = 0; currentLevelIndex = 1;
while q is not empty do:
  u = q.pop()
  print currentLevelIndex and u
  decrement nodesInCurrentLevel
  for every child v of u do:
    increment nodesInNextLevel
    q.push(v)
  if nodesInCurrentLevel is 0 do:
    nodesInCurrentLevel = nodesInNextLevel
    nodesInNextLevel = 0
    increment currentLevelIndex

Of course, this would print the output as Level 2:0 Level 2:2, etc. You can store current level nodes in a temporary list within the loop and print as appropriate. 当然,这会将输出打印为Level 2:0 Level 2:2等。您可以将当前级别节点存储在循环内的临时列表中并根据需要进行打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM