DEV Community

Discussion on: Walking a tree (depth first search)

Collapse
 
konstantinosblatsoukasrepo profile image
Konstantinos Blatsoukas

Hi Nam,

Thanks for the comment!

  • in the bfs implementation you need to utilize a queue and a tree (see my previous article dev.to/konstantinosblatsoukasrepo/...)

  • there will be a need to mark the visited nodes in case of graph (in order to avoid cycles). In a tree, you don't have that problem (there is no cycle)

  • if that was a bfs the nodes will be traversed layer by layer, in the above code that doesn't happens

  • now, a stack is a data structure that is like a stack of plates, possible operatios:

  1. Put something in the top of a stack
  2. Remove something from the top

In this case, I used a js array as a stack, I was adding something at the end of the array and removing something from the end of the array

Collapse
 
konstantinosblatsoukasrepo profile image
Konstantinos Blatsoukas • Edited

Yes you are right I have not explained the tree object, I will do that! Thanks!

Collapse
 
namhle profile image
Nam Hoang Le

Oops, I’m wrong at some points. Btw, thanks for sharing.
Cheers.

Collapse
 
konstantinosblatsoukasrepo profile image
Konstantinos Blatsoukas

Nam, I just added how a tree node looks like! thanks again for your comment

Thread Thread
 
namhle profile image
Nam Hoang Le

That is a ideal structure, and does not exist in reality world. I believe that we will have adjacency list only.