Is BFS preorder or postorder?

Author: yong

Nov. 28, 2023

260

0

0

Tags: Hardware

There are two methods to traverse trees: depth-first search (DFS) and breadth-first search (BFS).

DFS is the most common way, and it is performed by traversing to the leaf nodes. It goes as deep as it can and then moves on to the next branch. DFS is implemented using a stack or recursion.

BFS on the other hand traverse the tree by level and can also be called level-order traversal. So it will go all the way through level 1, then level 2, and follow this path until it reaches the last level. BFS is used to find the shortest path to a node.

DFS is much more common with trees, so we will discuss how it's done here, and then discuss DFS vs. BFS in more depth in the graph data structure section.

There are three ways to traverse a tree using DFS: in-order traversal, pre-order traversal, and post-order traversal. Each of these implementations are DFS and explore down a path fully. The only difference is the order in which they use the current node's data.

In this article, we'll use the recursive solution to teach the concept because it is the best for gaining the initial intuition, and then in the interview questions, you'll get exposure to the iterative solutions as well.

Traversal Implementations

For all the cases, we will only consider a binary tree that has a left and right pointer.

You will notice that the code for each solution below looks very similar - almost identical. The only difference is when we "visit" a node which is when we use the current node's data in our algorithm.

Since we are using recursion, we are building up a stack of function calls, and it will unwind when we reach the leaf nodes which is the base case. What determines the order is if we "visit" the node before the recursive call, after visiting the left node recursively, or after visiting both the left and right nodes recursively.

The input function will be the following node implementation:

 

In our case we will consider visiting the node as just printing its data:

 

In-Order Traversal

In-order traversal is the most common and visits the nodes in ascending order. If it were a binary search tree, this will start with the smallest value at the left-most node and end at the largest value at the right-most node.

Use in-order traversal if you need to utilize the nodes as if they are in a sorted linear order.

 

Pre-Order Traversal

Pre-order traversal will always visit the current node before visiting its children. The root is the first node visited. It follows the left path and visits each node as it encounters them. After that, it follows the right paths and still visits the nodes as it encounters them before continuing on with the path.

Use this if you want to explore a given level before reaching the leaf node of a path.

Suggested reading:
Hardware
What is profiling steel?
Optimal Pressure for Torch Cutting: A Comprehensive Guide
 

Post-Order Traversal

For post-order traversal, you visit a node's children before visiting that node. This means that you will use it when you want to reach the leaves first. You visit the entire left subtree, then the entire right subtree, and then the root node of the subtree.

 

Breadth-First Search

With breadth-first search (BFS) you use a queue data structure instead of a stack. It traverses across each level before going deeper into the graph.

We'll explore BFS further in the graph section as well.

 

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:

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!

Is BFS preorder or postorder?

binary tree - Breadth First Search Traversal VS Pre-order ...

Comments

Please Join Us to post.

0

0/2000

Guest Posts

If you are interested in sending in a Guest Blogger Submission,welcome to write for us.

Your Name: (required)

Your Email: (required)

Subject:

Your Message: (required)

0/2000