12

For a binary tree, is Breadth First Search traversal (BFS) the same as Pre-order traversal? I am a little bit confused by these two different types of traversals. Can anyone please explain this to me? Additionally, how does Pre-order traversal compare to Depth First Search traversal (DFS)?

Thank you so much!

Nathan
  • 7,853
  • 4
  • 27
  • 50
Liu
  • 413
  • 4
  • 11
  • 1
    Thank you for your efforts to modify my question for the good of other users! – Liu Mar 26 '19 at 15:53

2 Answers2

17

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!

Nathan
  • 7,853
  • 4
  • 27
  • 50
  • 2
    Great answer. In addition, something that may help to visualize the difference is that a pre-order search is typically implemented using a Stack data structure, whilst BFS typically uses a Queue. This is because BFS prioritizes a nodes neighbors, whilst preorder DFS prioritizes a nodes children. – ifiore Mar 19 '19 at 14:52
  • 1
    @ifiore Thank you, yes you're correct :) DFS uses a stack and BFS uses a queue for their underlying implementations. – Nathan Mar 19 '19 at 14:59
  • thank you! But I didn't realized that I got my question wrong until I read your answers. My right questions is DFT the same as preorder traverse? – Liu Mar 19 '19 at 15:42
  • Yes, pre-order traversal is a form of depth first search. I put that in my answer as well. – Nathan Mar 19 '19 at 15:55
8

DFS types are pre-order , in-order and post-order;

DFS is like - first go for my descendants (grand-grand-..child) then I will see my siblings descendants. BFS is like - first go for my siblings then their child then their child and so on.

pre-order DFS technique is generally used in graph traversal .

in-order traversal only in binary tree.(tree is special kind of graph)

BFS is a level order traversal in the case of tree.

traversing in a binary tree

These four are different techniques of traversal and results are also different. Sometimes their result might be same so don't get confused, check for different tree and you will find out difference.

UNREAL
  • 430
  • 5
  • 11
  • I think In the above answer the Postorder Traversal is incorrect, the correct Postorder Traversal would be : 4,5,2,6,7,3,1 – pavan.vn101 Jan 17 '22 at 17:17
  • Yes you are right, I will correct it, actually if you traverse like Right child then left child then print It will show 7635421 – UNREAL Jul 05 '22 at 04:38