int main(int argc, const char * argv[]) { node<int> *root = new node<int>; root->data = 0; node<int> *child1 = new node<int>; child1->data = 1; node<int> *child5 = new node<int>; child5->data = 5; node<int> *child3 = new node<int>; child3->data = 3; node<int> *child4 = new node<int>; child4->data = 4; node<int> *child2 = new node<int>; child2->data = 2; node<int> *child12 = new node<int>; child12->data = 12; node<int> *child6 = new node<int>; child6->data = 6; node<int> *child9 = new node<int>; child9->data = 9; node<int> *child8 = new node<int>; child8->data = 8; node<int> *child7 = new node<int>; child7->data = 7; child6->left = child4; child4->parent = child6; child6->right = child9; child9->parent = child6; child4->left = child3; child3->parent = child4; child4->right = child5; child5->parent = child4; child3->left = child1; child1->parent = child3; child9->left = child7; child7->parent = child9; printTree(child6); node<int> *ancestor = findCommonAncestor(child1, child9); if ( ancestor ) std::cout << "\nfound common ancestor = " << ancestor->data << "\n"; else std::cout << "\nno common ancestor found\n"; ancestor = findCommonAncestor(child9, child1); if ( ancestor ) std::cout << "\nsearching the other way = " << ancestor->data << "\n"; else std::cout << "\nno common ancestor found\n"; return 0; }
int main() { Node* root = new Node(5); Node* node1 = new Node(1); Node* node2 = new Node(2); Node* node3 = new Node(3); Node* node4 = new Node(4); Node* node6 = new Node(6); Node* node7 = new Node(7); Node* node8 = new Node(8); Node* node9 = new Node(9); Node* node10 = new Node(10); root->left = node3; root->right = node8; node3->left = node1; node1->right = node2; node3->right = node4; node8->left = node6; node6->right = node7; node8->right = node9; node9->right = node10; Node* commonAncestor = findCommonAncestor(root, node7, node9); std::cout << "Common ancestor is located at " << commonAncestor->val << std::endl; return 0; }
static void removeNodes(DFNode *beginNode, DFNode *endNode) { CommonAncestorInfo common = findCommonAncestor(beginNode,endNode); assert(common.commonAncestor != NULL); assert(common.beginAncestor != NULL); assert(common.endAncestor != NULL); DFNode *begin = beginNode; while (begin != common.beginAncestor) { DFNode *parent = begin->parent; if (begin->next != NULL) DFRemoveNode(begin->next); else begin = parent; } DFNode *end = endNode; while (end != common.endAncestor) { DFNode *parent = end->parent; if (end->prev != NULL) DFRemoveNode(end->prev); else end = parent; } if (common.beginAncestor != common.endAncestor) { while (common.beginAncestor->next != common.endAncestor) DFRemoveNode(common.beginAncestor->next); } while ((beginNode != NULL) && (beginNode->first == NULL) && (beginNode->tag != WORD_DOCUMENT)) { DFNode *parent = beginNode->parent; DFRemoveNode(beginNode); beginNode = parent; } while ((endNode != NULL) && (endNode->first == NULL) && (endNode->tag != WORD_DOCUMENT)) { DFNode *parent = endNode->parent; DFRemoveNode(endNode); endNode = parent; } }
void PhylogenyViewer::classifySignal(vector<TaxonIdentifier>*taxons,int kmerCoverage,Vertex*vertex,Kmer*key){ // given a list of taxon, // place the kmer coverage somewhere in // the tree // // case 1. // if there are 0 taxons, this is unknown stuff // // case 2. // if there is one taxon, place the coverage on it // // case 3. // if there is at least 2 taxons and they all have the same parent // // case 4. // if there is at least 2 taxons and they don't have the same parent // but they have a common ancestor if(taxons->size()==0){ m_unknown+=kmerCoverage; // case 1. }else if(taxons->size()==1){ TaxonIdentifier taxon=taxons->at(0); m_taxonObservations[taxon]+=kmerCoverage; // case 2. }else{ // more than 1 #ifdef ASSERT assert(taxons->size()>1); #endif // a taxon can only have one parent, // simply check if they have all the same parent... map<TaxonIdentifier,int> parentCount; int found=0; for(int i=0;i<(int)taxons->size();i++){ TaxonIdentifier taxon=taxons->at(i); if(m_treeParents.count(taxon)==0){ cout<<"Warning: Taxon "<<taxon<<" is not in the tree"<<endl; continue; } TaxonIdentifier parent=getTaxonParent(taxon); parentCount[parent]++; found++; } if(parentCount.size()==1){ // only 1 common ancestor, easy #ifdef ASSERT if(!(parentCount.begin()->second == found)){ cout<<"Error: taxons: "<<taxons->size()<<", parentCount: "<<parentCount.size()<<" 1 element with "<<parentCount.begin()->second<<" taxons"<<endl; } assert(parentCount.begin()->second == found); #endif TaxonIdentifier taxon=parentCount.begin()->first; m_taxonObservations[taxon]+=kmerCoverage; // case 3. return; } if(parentCount.size()==0){ cout<<"Error, no parents, returning now."<<endl; return; } // at this point, we have more than one taxon and // they don't share the same parent // since we have a tree, find the nearest common ancestor // in the worst case, the common ancestor is the root #ifdef ASSERT assert(parentCount.size()>1); #endif TaxonIdentifier taxon=findCommonAncestor(taxons); // classify it m_taxonObservations[taxon]+=kmerCoverage; // case 4. } }