title: How to Traverse an NLTK Tree object?
body: <p>Given a bracketed parse, I could convert it into a Tree object in NLTK as such:</p>
  
  <pre><code>&gt;&gt;&gt; from nltk.tree import Tree
  &gt;&gt;&gt; s = '(ROOT (S (NP (NNP Europe)) (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends)))) (. .)))'
  &gt;&gt;&gt; Tree.fromstring(s)
  Tree('ROOT', [Tree('S', [Tree('NP', [Tree('NNP', ['Europe'])]), Tree('VP', [Tree('VBZ', ['is']), Tree('PP', [Tree('IN', ['in']), Tree('NP', [Tree('DT', ['the']), Tree('JJ', ['same']), Tree('NNS', ['trends'])])])]), Tree('.', ['.'])])])
  </code></pre>
  
  <p>But when I try to traverse it, I can only access the top most Tree:</p>
  
  <pre><code>&gt;&gt;&gt; for i in Tree.fromstring(s):
  ...     print i
  ... 
  (S
    (NP (NNP Europe))
    (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends))))
    (. .))
  &gt;&gt;&gt; for i in Tree.fromstring(s):
  ...     print i, i.label()
  ... 
  (S
    (NP (NNP Europe))
    (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends))))
    (. .)) S
  &gt;&gt;&gt; 
  </code></pre>
  
  <p>I could go one level deep as follows:</p>
  
  <pre><code>&gt;&gt;&gt; for i in Tree.fromstring(s):
  ...     print i.subtrees()
  ... 
  &lt;generator object subtrees at 0x7f1eb1571410&gt;
  &gt;&gt;&gt; for i in Tree.fromstring(s):
  ...     for j in i.subtrees():
  ...             print j
  ... 
  (S
    (NP (NNP Europe))
    (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends))))
    (. .))
  (NP (NNP Europe))
  (NNP Europe)
  (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends))))
  (VBZ is)
  (PP (IN in) (NP (DT the) (JJ same) (NNS trends)))
  (IN in)
  (NP (DT the) (JJ same) (NNS trends))
  (DT the)
  (JJ same)
  (NNS trends)
  (. .)
  </code></pre>
  
  <p>But is there a way to traverse all subtrees depth wise?</p>
  
  <p><strong>How should one traverse a tree in NLTK?</strong></p>
  
  <p><strong>How to traverse all subtrees in NLTK?</strong></p>