Depth-first search algorithm in Java