def traverse_post_order(start_node, get_children, filter_func=None):
 class _Node(object, ):
  '\n        Wrapper node class to keep an active children iterator.\n        An active iterator is needed in order to determine when all\n        children are visited and so the node itself should be visited.\n        '
  def __init__(self, node, get_children):
   self.node = node
   self.children = iter(get_children(node))
 filter_func = (filter_func or (lambda __: True))
 stack = deque([_Node(start_node, get_children)])
 visited = set()
 while stack:
  current = stack[(-1)]
  if ((current.node in visited) or (not filter_func(current.node))):
   stack.pop()
   continue
  try:
   next_child = current.children.next()
  except StopIteration:
   (yield current.node)
   visited.add(current.node)
   stack.pop()
  else:
   stack.append(_Node(next_child, get_children))

def traverse_post_order ( start_node , get_children , filter_func = None ) : class _Node ( object , ) : def __init__ ( self , node , get_children ) : self . node = node self . children = iter ( get_children ( node ) ) filter_func = ( filter_func or ( lambda __ : True ) ) stack = deque ( [ _Node ( start_node , get_children ) ] ) visited = set ( ) while stack : current = stack [ ( - 1 ) ] if ( ( current . node in visited ) or ( not filter_func ( current . node ) ) ) : stack . pop ( ) continue try : next_child = current . children . next ( ) except StopIteration : ( yield current . node ) visited . add ( current . node ) stack . pop ( ) else : stack . append ( _Node ( next_child , get_children ) )
generator for yielding nodes of a tree in a post-order sort .