Example #1
0
bool subTree(TreeNode *r1, TreeNode *r2)
{
	if (NULL == r1)
	{
		return false;
	}
	if (r1->val == r2->val && matchTree(r1, r2))
	{
		return true;
	}
	return (subTree(r1->left, r2) || subTree(r1->right, r2));
}
Example #2
0
void KDTree::build(const vec2& iLats, const vec2& iLons) {
    mLats = iLats;
    mLons = iLons;
    if(iLats.size() != iLons.size())
        Util::error("Cannot initialize KDTree, lats and lons not the same size");

    size_t nLat = iLats.size();
    if(nLat == 0)
        Util::error("Cannot initialize KDTree, no valid locations");

    size_t nLon = iLats[0].size();
    if(nLon == 0)
        Util::error("Cannot initialize KDTree, no valid locations");

    indexdVec lons(nLon*nLat);

    indexdVec::iterator currLon = lons.begin();
    size_t to = -1;
    for(size_t i = 0; i < iLats.size(); ++i) {
        for(size_t j = 0; j < iLats[0].size(); ++j) {
            if(Util::isValid(iLons[i][j]) && Util::isValid(iLats[i][j])) {
                *(currLon++) = Indexed(iLons[i][j], iLats[i][j], i, j);
                ++to;
            }
        }
    }

    if(to == -1) {
        Util::error("Cannot initialize KDTree, no valid locations");
    }
    if(to >= 0) subTree(lons, 0, to, true, NULL, mRoot);
}
Example #3
0
void KDTree::subTree(indexdVec& iLonLat,
                     const size_t from,
                     const size_t to,
                     const bool xsection,
                     const TreeNode* parent,
                     unode& root) {

    root.reset(new KDTree::TreeNode());

    root->parent = parent;
    root->xsection = xsection;

    size_t len = to - from + 1;

    if(len == 1) {
        root->lon = iLonLat[from].lon;
        root->lat = iLonLat[from].lat;
        root->ipos = iLonLat[from].index1;
        root->jpos = iLonLat[from].index2;
        return;
    }

    size_t med = from + len/2;
    if(xsection) {
        std::sort(iLonLat.begin() + from, iLonLat.begin() + to + 1, compareLons);
        while(med < to && iLonLat[med].lon == iLonLat[med+1].lon) {
            assert(iLonLat.size() > med+1);
            ++med;
        }
    }
    else {
        std::sort(iLonLat.begin() + from, iLonLat.begin() + to + 1, compareLats);
        while(med < to && iLonLat[med].lat == iLonLat[med+1].lat) {
            assert(iLonLat.size() > med+1);
            ++med;
        }
    }

    root->lon = iLonLat[med].lon;
    root->lat = iLonLat[med].lat;
    root->ipos = iLonLat[med].index1;
    root->jpos = iLonLat[med].index2;

    if(med > from) subTree(iLonLat, from, med - 1, !xsection, root.get(), root->left);
    if(med < to) subTree(iLonLat, med + 1, to, !xsection, root.get(), root->right);

}
Example #4
0
bool containTree(TreeNode *t1, TreeNode *t2)
{
	if (NULL == t2)
	{
		return true;
	} else
	{
		return subTree(t1, t2);
	}
}
Example #5
0
int main(int argc, char * argv[]) {
	btree_t t1 = new btree();
	t1->value = 1;

	btree_t t2 = new btree();
	t2->value = 2;
	btree_t t3 = new btree();
	t3->value = 1;
	t2->leftChild = t3;
	std::cout << subTree(t2, t1) << std::endl;
 	return 0;
}
Example #6
0
void processFileRemoved(tree<FileInfo>::iterator parentIt,
                        const FileChangeEvent& fileChange,
                        bool recursive,
                        tree<FileInfo>* pTree,
                        std::vector<FileChangeEvent>* pFileChanges)
{
   // search for a child with this path
   tree<FileInfo>::sibling_iterator remIt = findFile(pTree->begin(parentIt),
                                                     pTree->end(parentIt),
                                                     fileChange.fileInfo());

   // only generate actions if the item was found in the tree
   if (remIt != pTree->end(parentIt))
   {
      // if this is folder then we need to generate recursive
      // remove events, otherwise can just add single event
      if (recursive && shouldTraverse(*remIt))
      {
         tree<FileInfo> subTree(remIt);
         std::for_each(subTree.begin(),
                       subTree.end(),
                       boost::bind(addEvent,
                                   FileChangeEvent::FileRemoved,
                                   _1,
                                   pFileChanges));
      }
      else
      {
         // use the previous FileInfo for the event payload (since the
         // passed FileInfo might not have a correct value for isDirectory
         // since we couldn't read it from the filesystem)
         pFileChanges->push_back(FileChangeEvent(FileChangeEvent::FileRemoved,
                                                 *remIt));
      }

      // remove it from the tree
      pTree->erase(remIt);
   }
}
Example #7
0
void extrait_outgroup(string inFile,string outFile,list<string> &outgroups,int nb){
     int n;//the number of internal nodes
	 int m;//the number of branches
     int n1,m1;
    counting(inFile,n,m,nb);
    int *P = new int[m+1];//tableau de predecesseurs
    double *B = new double[m+1];//branch lengths
    string *Support = new string[m+1];
    string* Labels = new string[m+1];
	FILE * tree = fopen(inFile.c_str(),"rt");
	if (tree==NULL) cout<<"Can not open the tree file"<<endl;
	else{
         FILE * w = fopen(outFile.c_str(),"wt");
         bool rooted=false;
         for (int y=1;y<=nb;y++){
             if (rooted){
                n++;
                m++;
             }
             tree2dataS(tree,n,m,P,B,Support,Labels);
             int * Suc1= new int[n];
             int * Suc2= new int[n];
             computeSuc(P,Suc1,Suc2,m+1,n);
             list<int> P1;
             list<double> B1;
             list<string> Support1;
             list<string> Labels1;
             list<double> T1;
             int s;
             if (m==2*n){
                rooted=true;
                rooted2unrooted(n,m,P,Suc1,Suc2,B,Labels);
                m--;
                n--;
                computeSuc(P,Suc1,Suc2,m+1,n);
             }
             s= computeSuc_unrooted(P,Suc1,Suc2,m+1,n);
             list<int> out;
             list<int> in;
             for (int i=n;i<=m;i++){
                 bool flag = true;
                 for (list<string>::iterator iter=outgroups.begin();iter!=outgroups.end();iter++){
		             if (Labels[i].compare(*iter)==0){
                        out.push_back(i);                   
                        flag=false;
                        break;
                     }
                 }
                 if (flag) in.push_back(i);
             }
             if (out.size()==0){
                cout<<"Tree "<<y<<": the tree does not contain any outgroup"<<endl;  
                exit( EXIT_SUCCESS );
             }
             else{
                 int *P_new= new int[m+1];
                 int *Suc1_new= new int[n];
                 int *Suc2_new= new int[n];
                 double *B_new= new double[m+1];
                 string* Support_new = new string[m+1];
                 int t=out.front();
                 int t1=in.front();
                  if (out.size()==1){
                     reroot(n,m,t,P,s,Suc1,Suc2,B,Support,P_new,Suc1_new,Suc2_new,B_new,Support_new);
                     subTree(n,m,P[t],P_new,Suc1_new,Suc2_new,B_new,Support_new,Labels,P1,B1,Support1,Labels1);
                     int *Po=new int[m];
                     listToArray(P1,Po);
                     double *Bo=new double[m];
                     listToArray(B1,Bo);
                     string* Supporto=new string[m];
                     listToArray(Support1,Supporto);
                     string* Labelso = new string[m];
                     listToArray(Labels1,Labelso);
                     int *Suc1o=new int[n];
                     int *Suc2o=new int[n];
                     computeSuc(Po,Suc1o,Suc2o,m,n);
                     newicktree(n,Po,Suc1o,Suc2o,Labelso,Bo,Supporto,w);
                      delete[]  Po;
                      delete[]  Bo;
                      delete[] Labelso;
                      delete[] Suc1o;
                      delete[] Suc2o;
                      delete[] Supporto;
                  }
                  else{
	                   bool flag = false;
	                   while (!flag && P[t]!=-1){
                             t = P[t];
                              flag=true;
                              for (list<int>::iterator ia=out.begin();ia!=out.end();ia++){
                                  int j=*ia; 
                                  if (!isAncestor(P,t,j)) {
                                     flag=false;break;
                                  }
                              }
                       }//t is lca of outgroups
                       if (P[t]!=-1){//lca of outgroups is not the root
                          reroot(n,m,t,P,s,Suc1,Suc2,B,Support,P_new,Suc1_new,Suc2_new,B_new,Support_new);
                          subTree(n,m,P[t],P_new,Suc1_new,Suc2_new,B_new,Support_new,Labels,P1,B1,Support1,Labels1);
                       }
                       else{//lca of outgroups is the root
	                        bool flag = false;
	                        while (!flag && P[t1]!=-1){
                                  t1 = P[t1];
                                  flag=true;
                                  for (list<int>::iterator ia=in.begin();ia!=in.end();ia++){
                                      int j=*ia;
                                      if (!isAncestor(P,t1,j)) {flag=false;break;}
                                  }
                            }
                           //t1 is lca of ingroups
                            if (P[t1]==-1){//lca of ingroups is the root
                               cout<<"Tree "<<y<<": The outgroups are not separated from the ingroups"<<endl; 
                               exit( EXIT_FAILURE );
                            }
                            else{//lca of ingroups is not the root
                                reroot(n,m,t1,P,s,Suc1,Suc2,B,Support,P_new,Suc1_new,Suc2_new,B_new,Support_new);
                                subTree(n,m,t1,P_new,Suc1_new,Suc2_new,B_new,Support_new,Labels,P1,B1,Support1,Labels1);
                          }
                      }
                      m1=(int)P1.size()-1;
                      n1=m1/2;
                      if ((n1+out.size())==n+1){
                              int *Po = new int[m1+1];
                              listToArray(P1,Po);
                              double *Bo = new double[m1+1];
                              listToArray(B1,Bo);
                              string* Supporto = new string[m1+1];
                              listToArray(Support1,Supporto);
                              string* Labelso = new string[m1+1];
                              listToArray(Labels1,Labelso);
                              int *Suc1o = new int[n1];
                              int *Suc2o = new int[n1];
                              computeSuc(Po,Suc1o,Suc2o,m1+1,n1);
                              newicktree(n1,Po,Suc1o,Suc2o,Labelso,Bo,Supporto,w);
                              delete[]  Po;
                              delete[]  Bo;
                              delete[]  Supporto;
                              delete[] Labelso;
                              delete[] Suc1o;
                              delete[] Suc2o;
                      }
                      else {
                              cout<<"Tree "<<y<<": The outgroups are not separated from the ingroups"<<endl;
                              exit( EXIT_FAILURE );
                      }
                      delete[] P_new;
                      delete[] Suc1_new;
                      delete[] Suc2_new;
                      delete[] B_new;
                      delete[] Support_new;
                  }
             delete[] Suc1;
             delete[] Suc2;
             }
             
         }
         fclose(tree);
        fclose(w);
    }
    delete[]  P;
    delete[]  B;
    delete[]  Support;
    delete[] Labels;
}