广度优先搜索遍历VS顺序遍历VS深度优先搜索遍历

时间:2019-03-19 14:17:08

标签: binary-tree breadth-first-search tree-traversal preorder

对于二叉树,宽度优先搜索遍历(BFS)是否与预定遍历相同?这两种不同类型的遍历让我有些困惑。有人可以向我解释一下吗?另外,预订遍历深度优先搜索遍历(DFS)有什么区别?

非常感谢!

2 个答案:

答案 0 :(得分:1)

No, pre-order traversal is actually a form of Depth-First-Search (DFS) traversal. There are three different forms of DFS, namely:

  1. Pre-Order
  2. In-Order
  3. Post-Order

To prove that Breadth-First-Search (BFS) traversal isn't the same as pre-order traversal I will show a counter example below:

To be clear a binary tree isn't the same as a binary search tree, namely a binary tree can be defined as:

Binary Tree - A tree whose elements have at most 2 children is called a binary tree. Note there is no mentioning of the ordering of the children.

Ok now to the counter-example, take the following simple binary tree:

counter example binary tree

For a pre-order traversal the nodes are visited in the following order: Pre-Order: [1,2,4,3]

now for Breadth-First-Search traversal the nodes are visited in the following order:

BFS: [1,2,3,4]

Note: pre-order traversal is different from the BFS traversal.

For more information on the different tree traversals check out this link

Hopefully, that helps!

答案 1 :(得分:1)

二叉搜索树中的预排序和BFS结果相同,但简单二叉树中的结果不同。