For traversal, if we’re going to implement such a traversal or a search, there’s no doubt that the things we want to ensure are:

  1. Every node has to be accessed
  2. Each node is accessed only once
  3. The order in which nodes are accessed is variable
  4. Depth First: Depth First Search
  5. Our lines First Search
  • Depth-first traversal (DFS)

Depth-first traversal, the whole traversal order is no doubt that root node 1 always starts first, and it’s the same as going to the branch, so let’s just keep it simple and go from the far left, so it’s depth-first and it goes to the end.

  • Breadth-first traversal (BFS)

Breadth-first traversal of it doesn’t use recursion anymore, it doesn’t use a stack anymore, it uses something called a queue. You can think of it as a drop of water, dropping into position one, and then its ripples spread out layer by layer.

  • Comparison between the two: