title: How to write code that expresses level order for non binary tree?
body: <p>Here <code>t</code> denotes a tree. A tree can have a list of children. <code>act</code> is a behavior, like <code>print</code>. My code shows the correct order for all levels below root <code>t</code>. How can I modify the code to include <code>t</code> itself?</p>
  
  <pre><code>def levelorder_visit(t, act):
      """
      Visit every node in Tree t in level order and act on the node
      as you visit it.
  
      @param Tree t: tree to visit in level order
      @param (Tree)-&gt;Any act: function to execute during visit
  
      &gt;&gt;&gt; t = descendants_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7], 3)
      &gt;&gt;&gt; def act(node): print(node.value)
      &gt;&gt;&gt; levelorder_visit(t, act)
      0
      1
      2
      3
      4
      5
      6
      7
      """
      if t:
          for x in t.children:
              act(x)
          for i in t.children:
              levelorder_visit(i,act)
      ###prints out "1,2,3,4,5,6,7" for the above docstring example
  </code></pre>