Is post-order the same as DFS?
It probably depends on the definition and implementation of the depth-first
algorithm. The DefaultMutableTreeNode
class of the Java Swing's JTree
component has the following enumeration methods used for tree traversal:
- depthFirstEnumeration()
- postorderEnumeration()
- preorderEnumeration()
- breadthFirstEnumeration()
In Java Swing's implementation the depthFirstEnumeration
is the same
as the postOrderEnumeration
. My tests and the official documentation
confirms this.
Others can define what depth-first means differently. For instance, an article on Wikipedia states that pre-order and post-order traversals are specific types of a depth-first traversal. This would mean that the depth-first traversal is not a concrete traversal algorithm.
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.
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.
Is post-order the same as DFS?
Tree Traversal: In-Order, Pre-Order, Post-Order
311
0
0
All Comments (0)
Previous: Why China Is "The World's Factory" - Investopedia
Next: Tree Traversal: Inorder, Preorder, Postorder, and Level-order
If you are interested in sending in a Guest Blogger Submission,welcome to write for us!
Comments