Breadth-first and depth-first search in Ruby