'>>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
>>> t.treepositions() # doctest: +ELLIPSIS
[(), (0,), (0, 0), (0, 0, 0), (0, 1), (0, 1, 0), (1,), (1, 0), (1, 0, 0), ...]
>>> for pos in t.treepositions(\'leaves\'):
...     t[pos] = t[pos][::-1].upper()
>>> print(t)
(S (NP (D EHT) (N GOD)) (VP (V DESAHC) (NP (D EHT) (N TAC))))
:param order: One of: ``preorder``, ``postorder``, ``bothorder``,
``leaves``.'

def treepositions(self, order=u'preorder'):

    positions = []
    if (order in (u'preorder', u'bothorder')):
        positions.append(())
    for (i, child) in enumerate(self):
        if isinstance(child, Tree):
            childpos = child.treepositions(order)
            positions.extend((((i,) + p) for p in childpos))
        else:
            positions.append((i,))
    if (order in (u'postorder', u'bothorder')):
        positions.append(())
    return positions
