Implement a breadth-first search spanning tree function