I think this should do it. More 15 that 5 lines but it's very
straightforward. It's a pity we don't have a method to introduce a new
node at a particular point in a branch. (But maybe we have one?) Let me
know if you think there is a mistake. In any case it should be a good
template.
Could you please modify TreeTemplateTools.h and add the option to the
doc there ?
Nicolas
//
@ TreeTemplateTools.cpp :1011
void TreeTemplateTools::midRoot (TreeTemplate<Node>& tree, const string&
criterion, const bool force_branch_root)
@ TreeTemplateTools.cpp :1062
if(force_branch_root)
{
Node* orig_root = tree.getRootNode();
vector<Node*> root_sons = orig_root->getSons();
if(root_sons.size() >2)
{
Node* nearest =
root_sons.at(0);
for(vector<Node*>::iterator n = root_sons.begin(); n !=
root_sons.end(); ++n)
if((**n).getDistanceToFather() < nearest->getDistanceToFather())
nearest = *n;
const double d = nearest.getDistanceToFather();
Node* new_root = new Node();
new_root->setId( TreeTools::getMPNUId(tree, tree.getRootId()) );
orig_root.removeSon(nearest);
new_root.addSon(nearest);
orig_root.setDistanceToFather(d/2.);
nearest.setDistanceToFather(d/2.);
const vector<string> branch_properties = nearest->getBranchPropertyNames();
for(vector<string>::const_iterator p = branch_properties.begin();
p!=branch_properties.end(); ++p)
new_root->setBranchProperty(*p, *nearest->getBranchProperty(*p));
}
}