42 #ifndef _TREETEMPLATETOOLS_H_
43 #define _TREETEMPLATETOOLS_H_
55 template<
class N>
class TreeTemplate;
86 std::vector<N*> leaves;
87 getLeaves<N>(node, leaves);
98 static void getLeaves(N & node, std::vector<N *> & leaves)
102 leaves.push_back(& node);
104 for(
size_t i = 0; i < node.getNumberOfSons(); i++)
106 getLeaves<N>(* node.getSon(i), leaves);
118 std::vector<int> ids;
132 ids.push_back(node.
getId());
147 std::vector<int> ids;
148 const Node* n = &node;
151 ids.push_back(n->
getId());
189 if (node.getName() == name)
191 id =
new int(node.getId());
195 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
211 N* leaf = tree.getNode(leafName);
212 if (!leaf->hasFather())
213 throw Exception(
"TreeTemplateTools::dropLeaf(). Leaf is the only node in the tree, can't remove it.");
214 N* parent = leaf->getFather();
215 if (parent->getNumberOfSons() > 2)
218 parent->removeSon(leaf);
221 else if (parent->getNumberOfSons() == 2)
224 N* brother = parent->getSon(0);
225 if (brother == leaf) brother = parent->getSon(1);
226 if (!parent->hasFather())
229 if (leaf->hasDistanceToFather() && brother->hasDistanceToFather())
231 brother->setDistanceToFather(brother->getDistanceToFather() + leaf->getDistanceToFather());
233 brother->removeFather();
234 tree.setRootNode(brother);
240 N* gParent = parent->getFather();
241 if (brother->hasDistanceToFather() && parent->hasDistanceToFather())
243 brother->setDistanceToFather(brother->getDistanceToFather() + parent->getDistanceToFather());
245 size_t pos = gParent->getSonPosition(parent);
246 gParent->setSon(pos, brother);
254 throw Exception(
"TreeTemplateTools::dropLeaf. Parent node as only one child, I don't know what to do in that case :(");
268 if (!subtree->hasFather())
269 throw Exception(
"TreeTemplateTools::dropSubtree(). Trying to remove the full tree!");
270 N* parent = subtree->getFather();
271 if (parent->getNumberOfSons() > 2)
274 parent->removeSon(subtree);
277 else if (parent->getNumberOfSons() == 2)
280 N* brother = parent->getSon(0);
281 if (brother == subtree) brother = parent->getSon(1);
282 if (!parent->hasFather())
285 if (subtree->hasDistanceToFather() && brother->hasDistanceToFather())
287 brother->setDistanceToFather(brother->getDistanceToFather() + subtree->getDistanceToFather());
289 tree.setRootNode(brother);
295 N* gParent = parent->getFather();
296 if (brother->hasDistanceToFather() && parent->hasDistanceToFather())
298 brother->setDistanceToFather(brother->getDistanceToFather() + parent->getDistanceToFather());
300 size_t pos = gParent->getSonPosition(parent);
301 gParent->setSon(pos, brother);
309 throw Exception(
"TreeTemplateTools::dropSubtree. Parent node as only one child, I don't know what to do in that case :(");
323 std::vector<std::string> names = leaves;
324 for (
size_t n = names.size(); n > size; --n) {
327 names.erase(names.begin() + i);
340 std::vector<N *> nodes;
341 getNodes<N>(node, nodes);
352 static void getNodes(N & node, std::vector<N*> & nodes)
354 for(
size_t i = 0; i < node.getNumberOfSons(); i++)
356 getNodes<N>(*node.getSon(i), nodes);
358 nodes.push_back(& node);
369 std::vector<int> ids;
382 std::vector<int> ids;
399 ids.push_back(node.
getId());
425 std::vector<N *> nodes;
426 getInnerNodes<N>(node, nodes);
441 for(
size_t i = 0; i < node.getNumberOfSons(); i++)
443 getInnerNodes<N>(* node.getSon(i), nodes);
446 nodes.push_back(&node);
459 std::vector<int> ids;
477 ids.push_back(node.
getId());
488 std::vector<N*> nodes;
489 searchNodeWithId<N>(node, id, nodes);
501 for (
size_t i = 0; i < node.getNumberOfSons(); ++i)
503 searchNodeWithId<N>(*node.getSon(i), id, nodes);
505 if (node.getId() == id) nodes.push_back(&node);
515 if (node.
getId() == id)
535 if (node.
getId() == id)
556 if (node.getId() == id)
return true;
559 for(
size_t i = 0; i < node.getNumberOfSons(); i++)
575 std::vector<N*> nodes;
576 searchNodeWithId<N>(node, name, nodes);
588 for(
size_t i = 0; i < node.getNumberOfSons(); i++)
590 searchNodeWithName<N>(*node.getSon(i), name, nodes);
592 if(node.hasName() && node.getName() == name) nodes.push_back(&node);
603 if(node.hasName() & node.getName() == name)
return true;
606 for(
size_t i = 0; i < node.getNumberOfSons(); i++)
688 static unsigned int getDepths(
const Node& node, std::map<const Node*, unsigned int>& depths);
714 static double getHeights(
const Node& node, std::map<const Node*, double>& heights);
755 N* clone =
new N(node);
762 clone->addSon(cloneSubtree<N>(*node[i]));
775 for (
size_t i = 0; i < node->getNumberOfSons(); ++i)
777 N* son = node->getSon(i);
793 std::vector<int> sonsId = tree.
getSonsId(nodeId);
794 for (
size_t i = 0; i < sonsId.size(); i++)
796 clone->addSon(cloneSubtree<N>(tree, sonsId[i]));
799 std::vector<std::string> names;
801 for (
size_t i = 0; i < names.size(); i++)
803 clone->setNodeProperty(names[i], *tree.
getNodeProperty(nodeId, names[i]));
806 for (
size_t i = 0; i < names.size(); i++)
873 static
void scaleTree(
Node& node,
double factor) throw (NodePException);
942 Element() : content(), length(), bootstrap(), isLeaf(false) {}
997 static std::
string nodeToParenthesis(const
Node & node,
bool bootstrap, const std::
string & propertyName);
1141 static
void orderTree(
Node& node,
bool downward = true,
bool orderLeaves = false) {
1197 static OrderTreeData_
orderTree_(
Node& node,
bool downward,
bool orderLeaves);
1245 #endif //_TREETEMPLATETOOLS_H_