breadth-first and depth-first search
refactor