def breadth_first(tree, children=iter, maxdepth=(-1)):
 queue = deque([(tree, 0)])
 while queue:
  (node, depth) = queue.popleft()
  (yield node)
  if (depth != maxdepth):
   try:
    queue.extend(((c, (depth + 1)) for c in children(node)))
   except TypeError:
    pass

def breadth_first ( tree , children = iter , maxdepth = ( - 1 ) ) : queue = deque ( [ ( tree , 0 ) ] ) while queue : ( node , depth ) = queue . popleft ( ) ( yield node ) if ( depth != maxdepth ) : try : queue . extend ( ( ( c , ( depth + 1 ) ) for c in children ( node ) ) ) except TypeError : pass
traverse the nodes of a tree in breadth-first order .