Breadth-First-Search and Depth-First-Search are the two most popular tree/graph traversal algorithms. Both methods are going to traverse/visit the nodes in tree or graph, however they are different from the way it performs traversals. This difference determines which algorithm to use for a specific problem.
Hope this writing helps to learn the traversal techniques to some extent. Do let me know if any errors.
Traversal Techniques:

Breath First Search:
BFS is actually traversing the nodes level by level. That is, traversing the node first, then its children and then its children’s children. If you consider the following tree,

The root node is A. Its left and right children are B and C. Further its children are D,E,F and G. So the BFS of the above tree is
ABCDEFGHere Level Order traversal of a tree is going to be same as BFS since it traverse starting from level 0 till its last level. But not every BFS is level order traversal. For example,

Here the BFS traverse its left and right children first and then its children.
So the BFS traversal of the above tree is 12345. Whereas Level Order traverse level by level. So the level order traversal of the same tree is 13245.
Traversing the graph in BFS way is again going to be same. That visiting the nodes wider as the graph goes.
Consider the following graph,

Let’s traverse starting from Node 0, we first visit 0 and traversing its neighbouring nodes 1 and 2.


Further traversing the next wider level 2 and 5. And Finally 4.


Depth First Search:
As BFS traverse wide, DFS goes deep. It starts from first node, and goes deep in a path till it traverse the leaf node/the last node before start traversing its next path.

In the above tree, it first starts from root node 1, does deep to 2 and more deeper to 4. Node 4 has no children, so it is done with that path visits 5 and traverse back to 1 and starts visiting deep 3,6 and 7.
Further we can traverse in three different ways,
So the traversal of above tree is going to be,
Pre-Order: 1245367
Inorder: 4251637
Post-Order: 4526731DFS of graph is also goes deep till it traverse all the nodes in the graph.

Implementation:
BFS: FIFO (First In First Out)
In Breadth-First-Search, queue is going to be our hero. Here are the steps to implement BFS programmatically.

In the above tree,
public void BFS(TreeNode root)
{
Queue<Integer> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty())
{
int size = queue.size();
for (int i=0;i<size;i++)
{
TreeNode currentNode = queue.poll();
if(currentNode.left!=null) queue.add(currentNode.left);
if(currentNode.right!=null) queue.add(currentNode.right);
System.out.print(currentNode.val);
}
}
}
DFS: LIFO (Last In First Out)
We are going to get the help of stack, in order to traverse the tree/graph DFS way.

In above example,
public void DFSInOrder(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
TreeNode current = root;
stack.push(root);
while(!stack.isEmpty()) {
while(current.left != null) {
current = current.left;
stack.push(current);
}
current = stack.pop();
System.out.print(current.value);
if(current.right != null) {
current = current.right;
stack.push(current);
}
}
}When to use:
Few problems can be solved using both BFS and DFS.
BFS can be helpful when,
• We want to find the shortest path from any source to destination.
• You want to find there exist a path between two nodes.
• Bipartite Graph
• If you feel the solution exists in fewer levels instead of going deep..
DFS can be helpful when,
• We want to exhaust all possibilities, and check which is best.
• We need to count all possible paths from source to destination.
• Backtracking.
Here is the links for list of problems that can be solved using BFS and DFS.
Hope this article helped you in some way. Happy Coding!