'Generate all the subtrees of this tree, optionally restricted
to trees matching the filter function.
>>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
>>> for s in t.subtrees(lambda t: t.height() == 2):
...     print(s)
(D the)
(N dog)
(V chased)
(D the)
(N cat)
:type filter: function
:param filter: the function to filter all local trees'

def subtrees(self, filter=None):

    if ((not filter) or filter(self)):
        (yield self)
    for child in self:
        if isinstance(child, Tree):
            for subtree in child.subtrees(filter):
                (yield subtree)
