#include "componenttree.h"
-#include <map>
using namespace std;
ComponentTree::Node::Node(Node* parent, Component comp): _parent(parent), _comp(comp){
}
return leaves;
}
+
+void ComponentTree::branches (Node* n, map<Node*,size_t> & b){
+ if (n->_children.empty() && n->_parent != 0){
+ Node* p = n;
+ while (p->_parent != 0 && p->_children.size() < 2)
+ p = p->_parent;
+ ++b[p];
+ return;
+ }
+ for (vector<Node*>::iterator c = n->_children.begin();
+ c != n->_children.end(); ++c){
+ branches(*c,b);
+ }
+}
+
+size_t ComponentTree::shortBranches(){
+ map<Node*,size_t> br;
+ branches(_root,br);
+ if (_root->_children.size() < 2 && _root->_comp.sign != 0){
+ br[_root]++;
+ }
+ size_t sb = 0;
+ for (map<Node*,size_t>::iterator b = br.begin(); b != br.end(); ++b){
+ if (b->second == 1)
+ ++sb;
+ }
+ return sb;
+}
+
#define __COMPONENTTREE_H__
#include <vector>
+#include <map>
#include "misc.h"
class ComponentTree {
size_t countLeaves();
+ size_t shortBranches();
+
private:
//Disable these, at least for now.
void operator=(const ComponentTree&){};
void removeOriented(Node* n);
size_t countLeaves(Node* n);
+ void branches (Node* n, std::map<Node*,size_t> & b);
Node* _root;
CPPUNIT_TEST( testCreate );
CPPUNIT_TEST( testMakeUnoriented );
CPPUNIT_TEST( testCountLeaves );
+ CPPUNIT_TEST( testShortBranches );
CPPUNIT_TEST_SUITE_END();
protected:
CPPUNIT_ASSERT_EQUAL(false,n->_children.empty());
CPPUNIT_ASSERT_EQUAL((size_t)3u,t2.countLeaves());
}
+ void testShortBranches (){
+ GeneOrder go(_validPerm.begin(),_validPerm.end());
+ ComponentTree t(findComponents(go));
+ t.makeUnoriented();
+
+ CPPUNIT_ASSERT_EQUAL((size_t)0u,t.shortBranches());
+
+ GeneOrder go2(_validPerm4.begin(),_validPerm4.end());
+ ComponentTree t2(findComponents(go2));
+ t2.makeUnoriented();
+ ComponentTree::Node* n = t2._root;
+ //CPPUNIT_ASSERT_EQUAL((size_t)1u,t2.shortBranches());
+ }
};