DFS (Depth first search) starts with a given node and explores the first unexplored node it comes across before returning to itself again and exploring its remaining nodes (e.g: if the parent node
1has 2 children2, 3the DFS method will explore2and its children nodes before exploring3. It will printselfbefore exploring its children (so 1-(2,3)1->(2,3)will print 1,2,31,2,3))BFS (Breadth first search) works down a tree in a top-to-bottom manner (e.g: a graph with parent
1and children2, 3will print level 1 first (1) then level 2 (2, 3) and then level 3 (the children of nodes 2 and 3). The level of a given node is determined by the highest level it could appear on (e.g: if2is a child of an item on level 1level 1and level 4level 4, it would be printed as if it were a level 2level 2item)
dfhwze
- 14.2k
- 3
- 40
- 101