Ray*_*zer 3 algorithm binary-tree tree-traversal binary-search-tree
为什么通过根、左和右遍历树称为预排序?这不应该是有序的,因为根总是在第一位吗?
为什么这样称呼它对我来说没有意义,因为根始终是第一个元素。
我们总是有这样的限制:左孩子在右孩子之前被访问。
主要区别在于根在哪里。
如果根在两个孩子之前,我们称之为先序。(根,左,右)
如果根位于两个孩子之后,我们将其称为后序。(左、右、根)
如果根位于两个孩子之间,我们称其为有序。(左、根、右)
前缀是指什么时候应该放置根节点的内容。
给定这棵树,您可以用多种方式表示它:
[41, 20, 11, 29, 32, 65, 50, 91, 72, 99]
^ -------------- ------------------
| | |
| | |-----Right sub-tree
| |
| |----Left sub-tree
|
|------ Root of the tree
Run Code Online (Sandbox Code Playgroud)
在左子树和右子树子列表中,保留了预排序。
[11, 20, 29, 32, 41, 50, 65, 72, 91, 99]
-------------- | ------------------
| | |
| | |------- Right sub-tree
| |
| |---- Root of the tree
|
|----- Left sub-tree
Run Code Online (Sandbox Code Playgroud)
现在,列表的第一部分代表左子树,根放在后面,最后是右子树。在这里,inorder也保存在左右子树的子列表中。
有序遍历可以看作是从左到右的扫描。
[11, 32, 29, 20, 50, 72, 99, 91, 65, 41]
-------------- ------------------ |
| | |---- Root of the tree
| |
| |----- Right sub-tree
|
|------ Left sub-tree
Run Code Online (Sandbox Code Playgroud)
和其他的一样,根在最后,但左右子列表保持相同的后序属性。
此外,其他可能的遍历可以是
[41, 20, 65, 11, 29, 50, 91, 32, 72, 99]
| ------ -------------- ----------
| | | |-----Level 3
| | |
| | |----- Level 2
| |
| |------ Level 1
|
|----- Level 0 (aka, the root of the tree)
Run Code Online (Sandbox Code Playgroud)