Breadth First Search or Depth First Search



Applet source code

A breadth first search uses a queue (first in first out) to store the nodes it visits. The first node in the queue is the root, it is then removed and all of its children are placed in the queue, the next node from the queue is removed and all of its children are then placed in the queue. This contiues until all nodes have been visited. Depth first search is similar except that it uses a stack (last in first out) to store the nodes instead of a queue.