AlgoViz

Tree BFS (level-order traversal)

medium 16 min

Breadth-first search visits a tree one level at a time, left to right, using a queue.

Where it shows up

The same pattern appears across everyday software.

  • GPS navigation and game pathfinding

    On a grid or road network where every move costs the same, BFS finds the route with the fewest steps. Game AI uses it for unit movement on tile maps, and it is the starting point for weighted searches such as Dijkstra and A*.

  • Social networks

    Degrees of separation and "people you may know" are BFS from your profile: friends are level 1, friends of friends level 2. LinkedIn shows exactly these 1st, 2nd and 3rd connection levels.

  • Web crawlers

    A crawler starts from seed pages, queues every link it finds and visits pages in discovery order, with a visited set so it never fetches the same URL twice. Limiting the depth keeps it near the seeds.

  • Puzzle solving

    Sliding puzzles, a combination lock and the Rubik's cube are state graphs. BFS from the start state finds the solution with the minimum number of moves.

  • Broadcasting in networks

    A message flooded through a network spreads to direct neighbours first, then theirs, level by level. Routers and peer-to-peer systems use this, and hop counts are BFS distances.

  • Org charts, file trees and the DOM

    Showing a company hierarchy rank by rank, listing a folder tree one depth at a time, or finding the nearest matching element in an HTML document are all level-order traversals of a tree.