def reroot(self, tree):
children = {x.length:x for x in tree.seed_node.child_edges()}<EOL>tree.is_rooted=True<EOL>if len(children) > <NUM_LIT:2>:<EOL><INDENT>longest_edge_length = max(children.keys())<EOL>longest_edge = children[longest_edge_length]<EOL>tree.reroot_at_edge(longest_edge, <EOL>length1=(longest_edge_length/<NUM_LIT:2>), <EOL>length2=(longest_edge_length/<NUM_LIT:2>))<EOL>return tree<EOL><DEDENT>elif len(children) == <NUM_LIT:2>:<EOL><INDENT>raise BinaryTreeError("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>raise MalformedTreeError("<STR_LIT>")<EOL><DEDENT>
Reroot a tree: This small piece of code is intended to take a rooted 
tree that is NOT binary, that is to say, the root node has 3 or more
child nodes. To create a binary tree, this function will find the two 
longest distances between the child nodes, find which child node is 
common to them both, and reroot at the branch connecting that node 
to root. For example, the new root will be placed where the "x" is in 
the following example tree:
                              _
                           __|_     _
                          |_____x__|
                        --|__ _    |_
                             |_

Parameters
----------
tree: dendropy.Tree
    Tree to be rerooted

Raises
------
BinaryTreeError : If the input tree is already a binary tree