Abstracted shared code between breadth-first search and future
depth-first search into separate class.