Traversals
When we traverse a m-ary tree or a binary tree, we can either visit the deepest nodes first, or every node on the top-most level first. The former is called depth-first search (DFS), and the latter is called breadth-first search (BFS). For trees in particular, since trees are a subset of graphs, depth-first search can be done in three ways. These are pre-order, in-order, and post-order traversals. Hence, there are a total of four different forms of tree traversals.
#
Breadth-First Search (BFS)Typically speaking, breadth-first search is implemented in the form of a queue. This is because we want to visit all nodes on the current level before we move on to the next. With a queue, we can push the children of the current node to the end of the queue, so that the children can be processed last. To implement this, we can initialize a queue with the root node of the tree. As we perform whatever logic we need, we dequeue the current node from the queue, and enqueue the current node's children. This process is then repeated on to the children of the current node. It can be implemented both recursively and iteratively.
#
Depth-First Search (DFS)Unlike breadth-first search, depth-first search processes the child nodes first. Therefore, it makes the most sense to think of depth-first search in the form of a stack. We can push the children of the current node onto a stack, and pop them to perform whatever logic we need before we come back to the current node.
We can also implement depth-first search by using the call stack as our stack. This makes depth-first search much easier to implement with recursion.
#
Pre-order, In-order, and Post-order TraversalsFor trees, depth-first search can be done in three ways:
- Preorder: Visit the root node first, then the left subtree, then the right subtree.
- Inorder: Visit the left subtree first, then the root node, then the right subtree.
- Postorder: Visit the left subtree first, then the right subtree, then the root node.
Thanks to the author of this article, the implementation of these three forms of traversals can be simplified into something like the following: