def expand_polytomies(self, <EOL>map_attr="<STR_LIT:name>", <EOL>polytomy_size_limit=<NUM_LIT:5>,<EOL>skip_large_polytomies=False):
class TipTuple(tuple):<EOL><INDENT>pass<EOL><DEDENT>def add_leaf(tree, label):<EOL><INDENT>yield (label, tree)<EOL>if not isinstance(tree, TipTuple) and isinstance(tree, tuple):<EOL><INDENT>for left in add_leaf(tree[<NUM_LIT:0>], label):<EOL><INDENT>yield (left, tree[<NUM_LIT:1>])<EOL><DEDENT><DEDENT>for right in add_leaf(tree[<NUM_LIT:1>], label):<EOL><INDENT>yield (tree[<NUM_LIT:0>], right)<EOL><DEDENT><DEDENT>def enum_unordered(labels):<EOL><INDENT>if len(labels) == <NUM_LIT:1>:<EOL><INDENT>yield labels[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>for tree in enum_unordered(labels[<NUM_LIT:1>:]):<EOL><INDENT>for new_tree in add_leaf(tree, labels[<NUM_LIT:0>]):<EOL><INDENT>yield new_tree<EOL><DEDENT><DEDENT><DEDENT><DEDENT>n2subtrees = {}<EOL>for n in self.traverse("<STR_LIT>"):<EOL><INDENT>if n.is_leaf():<EOL><INDENT>subtrees = [getattr(n, map_attr)]<EOL><DEDENT>else:<EOL><INDENT>subtrees = []<EOL>if len(n.children) > polytomy_size_limit:<EOL><INDENT>if skip_large_polytomies:<EOL><INDENT>for childtrees in itertools.product(*[n2subtrees[ch] for ch in n.children]):<EOL><INDENT>subtrees.append(TipTuple(childtrees))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TreeError("<STR_LIT>" %n)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for childtrees in itertools.product(*[n2subtrees[ch] for ch in n.children]):<EOL><INDENT>subtrees.extend([TipTuple(subtree) for subtree in enum_unordered(childtrees)])<EOL><DEDENT><DEDENT><DEDENT>n2subtrees[n] = subtrees<EOL><DEDENT>return ["<STR_LIT>"%str(nw) for nw in n2subtrees[self]]<EOL>
Given a tree with one or more polytomies, this functions returns the
list of all trees (in newick format) resulting from the combination of
all possible solutions of the multifurcated nodes.

.. warning:

   Please note that the number of of possible binary trees grows
   exponentially with the number and size of polytomies. Using this
   function with large multifurcations is not feasible:

   polytomy size: 3 number of binary trees: 3
   polytomy size: 4 number of binary trees: 15
   polytomy size: 5 number of binary trees: 105
   polytomy size: 6 number of binary trees: 945
   polytomy size: 7 number of binary trees: 10395
   polytomy size: 8 number of binary trees: 135135
   polytomy size: 9 number of binary trees: 2027025

http://ajmonline.org/2010/darwin.php