[英]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 对于每次遍历,你
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): 只是为了可视化(输出为粗体):
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)
if(root is not NULL) if(root不为NULL)
endif 万一
Explanation : 说明:
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. 想象一下,你正在走路,从根开始。
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.