简体   繁体   English

二叉树双阶遍历

[英]Binary Tree Double Order Traversal

Can anyone explain me the double order traversal? 任何人都可以向我解释双顺序遍历吗?

        A
      /   \
     B     E
   /  \   /  \
  C   D  F    G

Double order Traversal output : ABCCBDDAEFFEGG 双顺序遍历输出:ABCCBDDAEFFEGG

I'm interested in explanation rather than the code. 我对解释而不是代码感兴趣。

Thanks 谢谢

Assuming you're interested in an explanation of what a double-order traversal does: 假设您对双顺序遍历的解释感兴趣:

For each traversal, you 对于每次遍历,你

  • Visit Node 访问节点
  • Traverse Left Child 穿越左边的孩子
  • Visit Node 访问节点
  • Traverse Right Child 横穿右儿童

That's all there is to it. 这里的所有都是它的。 In cases where you don't have a left child (like C, for example), the two "visit node" operations happen back to back, which is why you see two Cs in your output. 如果您没有左子(例如C),则两个“访问节点”操作会重复发生,这就是您在输出中看到两个C的原因。

Just to visualize it (with the output in bold): 只是为了可视化(输出为粗体):

  • Visit A: A 访问A: A
  • Traverse left child B 横过左边的孩子B.
    • Visit B: AB 访问B: AB
    • Traverse left child C 遍历左边的孩子C.
      • Visit C: ABC 访问C: ABC
      • No left child 没有留下的孩子
      • Visit C: ABCC 访问C: ABCC
      • No right child 没有合适的孩子
    • Traverse right child D 穿越右边的孩子D.
      • Visit D: ABCCD 访问D: ABCCD
      • No left child 没有留下的孩子
      • Visit D: ABCCDD 访问D: ABCCDD
      • No right child 没有合适的孩子
  • Visit A: ABCCDDA 访问A: ABCCDDA
  • Traverse right child E 穿越右边的孩子E.
    • Visit E: ABCCDDAE 访问E: ABCCDDAE

etc. 等等

Mainly it is a recursive approach to traverse a tree (here we are dealing with Binary tree) 主要是遍历树的递归方法(这里我们处理的是二叉树)

Here is an algorithm for Double Order Traversal : 这是一个双阶遍历的算法:

Algorithm DoubleOrder(root) 算法DoubleOrder(root)

  1. if(root is not NULL) if(root不为NULL)

    1. print(root) 打印(根)
    2. DoubleOrder (leftSubtree) //recursive call DoubleOrder (leftSubtree)//递归调用
    3. print(root) 打印(根)
    4. DoubleOrder (rightSubtree) // recursive call DoubleOrder (rightSubtree)//递归调用
  2. endif 万一

  3. end DoubleOrder 结束DoubleOrder

Explanation : 说明:

  1. Visit A & print it output : A 访问A并打印输出:A
  2. A have left subtree A已离开子树
    1. Visit B & print it output : AB 访问B并打印输出:AB
    2. B have left subtree B已经离开了子树
      1. visit C and print it output : ABC 访问C并打印输出:ABC
      2. C have no left subtree C没有左子树
      3. Visit C again and print it output : ABCC 再次访问C并打印输出:ABCC
      4. C have no subtree C没有子树
      5. return to B (all 4 statements have executed in case of C) 返回B(在C的情况下,所有4个语句都已执行)
    3. Visit B again and print it output : ABCCB 再次访问B并打印输出:ABCCB
    4. B have right subtree B有正确的子树
      1. visit D and print it output : ABCCBD 访问D并打印输出:ABCCBD
      2. D have no left subtree D没有左子树
      3. Visit D again and print it output : ABCCBDD 再次访问D并打印输出:ABCCBDD
      4. D have no subtree D没有子树
      5. return to B (all 4 statements have executed in case of D) 返回B(在D的情况下,所有4个语句都已执行)
    5. return to A (all 4 statements have executed in case of B) 返回A(B的情况下执行了所有4个语句)
  3. Visit A again and print it output : ABCCBDDA 再次访问A并打印输出:ABCCBDDA
  4. A have right subtree A有正确的子树
    1. Visit E & print it output : ABCCBDDAE 访问E并打印输出:ABCCBDDAE
    2. E have left subtree E已经离开了子树
      1. visit F and print it output : ABCCBDDAEF 访问F并打印输出:ABCCBDDAEF
      2. F have no left subtree F没有左子树
      3. Visit F again and print it output : ABCCBDDAEFF 再次访问F并打印输出:ABCCBDDAEFF
      4. F have no subtree F没有子树
      5. return to E (all 4 statements have executed in case of F) 返回E(F中所有4个语句都已执行)
    3. Visit E again and print it output : ABCCBDDAEFFE 再次访问E并打印输出:ABCCBDDAEFFE
    4. E have right subtree E有正确的子树
      1. visit G and print it output : ABCCBDDAEFFEG 访问G并打印输出:ABCCBDDAEFFEG
      2. G have no left subtree G没有左子树
      3. Visit G again and print it output : ABCCBDDAEFFEGG 再次访问G并打印输出:ABCCBDDAEFFEGG
      4. G have no subtree G没有子树
      5. return to E (all 4 statements have executed in case of G) 返回E(G的所有4个语句都已执行)
    5. return to A (all 4 statements have executed in case of E) 返回A(E的所有4个语句都已执行)
  5. Stop execution as we return back to main root of tree(as all 4 statement have executed in case of A) 当我们返回到树的主根时停止执行(因为在A的情况下所有4个语句都已执行)

Final output : ABCCBDDAEFFEGG 最终输出:ABCCBDDAEFFEGG

I hope it may helps you to understand overall concept! 我希望它可以帮助你理解整体概念! I am very happy to answer to stack overflow for the first time :) 我很高兴第一次回答堆栈溢出:)

Imagine you are walking, beginning at the root. 想象一下,你正在走路,从根开始。

  1. You are at A; 你在A;
  2. Walk to left child, you get to B; 走到左边的孩子,你到了B;
  3. Walk to left child, C; 走到左边的孩子,C;
  4. Dead end, you turn back, still C; 死路一条,你回头,还是C;
  5. Back at B; 回到B;
  6. Walk to right child, to D; 走到右边的孩子,到D;
  7. Dead end, turn back still D; 死路一条,转身还是D;

etc. 等等

This is just a traversal that kind of count both in and outs. 这只是一种遍历内外的遍历。

Between the visit of left and right children in a preorder traversal, you visit the root (because you must come back to it to walk further), and you can think of leaves as roots having no children, and null will just make you go back in no time (hence the two consecutive visits to leaves, and nodes only have right children). 在一个前序遍历的左右儿童访问之间,你访问根(因为你必须回到它进一步走),你可以认为叶子没有孩子的根,而null会让你回去在任何时候(因此连续两次访问叶子,节点只有正确的孩子)。

Recurse through: 递归:

1. Visit root of (sub)tree.
2. Visit left subtree.
3. Revisit root of (sub)tree.
4. Visit right subtree.

Offhand I can't think of an application for it, though I have done some truly freakish things with a pile of nodes that are in several trees (and linked lists) at once. 我无法想到它的应用程序,虽然我已经做了一些真正奇特的事情,一堆树上的节点(和链表)。

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

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